filter dictionary of type [String: [object]] for specific key in object arrays - swift

I'm showing users a tableview that looks like the contacts view in the sense that the data source is a dictionary of type [String: [User]] and so I'm showing section headers with the first letter of the Users name.
Now, I want to allow them to search by the users first name... but I can't get this to work.
These are the dictionaries... the friends will hold data like this ["A" :[Users whose first name start with an A]] and so on
var friends = [String: [User]]()
var filteredFriends = [String: [User]]()
This is my filtering code with searchText being the first name I want to search by
self.filteredFriends = self.friends.filter{friend in
return friend.firstName.lowercased().contains(searchText)}
What I want to do is make filteredFriends have all of the values of friends whose users first names start with the text.
How do I make this work with a dictionary like mine?
Thanks
More Info:
Class User {
var firstName: String?
init(name: String){
self.firstName = name
}
}
Sample scenario:
friends = ["A" : [User(name: "Anthony), User(name: "Arnold")], "B" : [User(name: "Barry")]]
filteredFriends = friends
searchText = "an"
Desired filteredFriends (end result) = ["A" : [User(name: "Anthony)]]

There's no simple way. You just have to cycle through the whole dictionary, like this:
// these are your initial conditions
class User {
let firstName: String
init(name: String) {
self.firstName = name
}
}
let friends : [String:[User]] =
["A" : [User(name: "Anthony"), User(name: "Arnold")],
"B" : [User(name: "Barry")]]
let searchText = "an"
// and here we go...
var filteredFriends = [String:[User]]()
for entry in friends {
let filt = entry.value.filter{$0.firstName.lowercased().hasPrefix(searchText)}
if filt.count > 0 {
filteredFriends[entry.key] = filt
}
}
Now filteredFriends contains the desired result.

Ideally, you would use map to transform the input dictionary to the output dictionary, by filtering each of its values (User arrays). Unfortunately, Dicationary.map returns [(Key, Value)] (an array of `(Key, Value) tuples), not a new Dictionary. I'll take the more conventional, iterative approach instead.
Note: For those who are about to circle jerk around using functional styles everywhere, who are about to suggest using reduce: no. Pull out a profiler, and look at how slow and power-consuming it is to generate dictionaries with reduce. Especially important on a mobile device with limited battery.
Here's how I would do it:
let allFriends = [String: [User]]()
var filteredFriends = [String: [User]]()
let searchText = "a".lowercased()
for (initial, allFriendsWithInitial) in allFriends {
if initial.lowercased() == searchText {
// don't even have to bother filtering `friends`
filteredFriends[initial] = allFriendsWithInitial
}
let filteredFriendsWithInitial = allFriendsWithInitial.filter{
$0.firstName.lowercased().contains(searchText)
}
if !filteredFriendsWithInitial.isEmpty {
filteredFriends[initial] = filteredFriendsWithInitial
}
}
print(filteredFriends)

Related

Print out properties that are not equal between two objects

Say I have a User class with three properties: name, email, and fruits. What's the most efficient way to determine the differences, and print them out in a dictionary alongside the property name?:
struct User: Equatable {
let name: String
let email: String
let fruits: [Fruit]
}
// Old user
let user = User(name: "Jane", email:"ja#ne.com", fruits: [.banana, .apple])
// Updated user
let updatedUser = User(name: user.name, email: user.email, fruits: [.apple, .peach])
// Looking for help writing a function that can efficiently find the changes + format them into a dictionary for Firebase etc:
let updatedProperties = updatesBetween(old: user, new: updatedUser)
// Output:
//["fruits": ["apple", "peach"]]
Take advantage of the fact that these objects are Equatable and check for their equality right off the bat before proceeding, and then go property by property and look for differences. Keep in mind that arrays care about order so [.apple, .banana] will be different from [.banana, .apple]. If you don't care about order then just sort them both before comparing (this may require additional steps depending on the contents of the array). You can also consider translating the arrays into sets before comparing (if the arrays don't contain duplicates).
func getDifferencesBetween(old: User, new: User) -> [String: Any] {
guard old != new else {
return [:]
}
var differences: [String: Any] = [:]
if old.name != new.name {
differences["name"] = new.name
}
if old.email != new.email {
differences["email"] = new.email
}
if old.fruits != new.fruits {
differences["fruits"] = new.fruits
}
return differences
}

Sorting plist data

I've got some repeating data in a plist, I then extract it into a dictionary and display it in my app. The only problem is that it needs to be in the same order i put it in the plist, but obviously, dictionary's can't be sorted and it comes out unsorted. So how would i achieve this?
My plist data repeats like this
I then convert that into a dictionary of type [Int : ItemType], ItemType is my data protocol, like this:
class ExhibitionUnarchiver {
class func exhibitionsFromDictionary(_ dictionary: [String: AnyObject]) throws -> [Int : ItemType] {
var inventory: [Int : ItemType] = [:]
var i = 0;
print(dictionary)
for (key, value) in dictionary {
if let itemDict = value as? [String : String],
let title = itemDict["title"],
let audio = itemDict["audio"],
let image = itemDict["image"],
let description = itemDict["description"]{
let item = ExhibitionItem(title: title, image: image, audio: audio, description: description)
inventory.updateValue(item, forKey: i);
i += 1;
}
}
return inventory
}
}
Which results in a dictionary like this:
[12: App.ExhibitionItem(title: "Water Bonsai", image: "waterbonsai.jpg", audio: "exhibit-audio-1", description: "blah blah blah"), 17: App.ExhibitionItem.....
I was hoping that since i made the key's Int's i could sort it but so far i'm having no luck. You might be able to tell i'm fairly new to swift, so please provide any info you think would be relevant. Thanks!
A Dictionary has no order. If you need a specific order, make root of type Array:
or sort it by the key manually:
var root = [Int:[String:String]]()
root[1] = ["title":"Hi"]
root[2] = ["title":"Ho"]
let result = root.sorted { $0.0 < $1.0 }
print(result)
prints:
[(1, ["title": "Hi"]), (2, ["title": "Ho"])]

Appending to dictionary of [character: [object]] returns 0 key/value pair

I'm trying to show a tableview similar to contacts with my list of users.
I declare a global variable of friends that will store the first character of a name and a list of users whose first name start with that
var friends = [Character: [User]]()
In my fetch method, I do this
for friend in newFriends {
let letter = friend.firstName?[(friend.firstName?.startIndex)!]
print(letter)
self.friends[letter!]?.append(friend)
}
After this, I should have my friends array with the first letter of the name and the users that fall in it; however, my friends dictionary is empty.
How do I fix this?
Edit: I'm following this tutorial and he doesnt exactly the same.. Swift: How to make alphabetically section headers in table view with a mutable data source
Rather than using Character as the key, use String. You need to be sure to init the [User] array for every new First Initial key you insert into groupedNames. I keep an array of groupedLetters to make it easier to get a section count
var groupedNames = [String: [User]]()
var groupedLetters = Array<String>()
func filterNames() {
groupedNames.removeAll()
groupedLetters.removeAll()
for friend in newFriends {
let index = friend.firstName.index(friend.firstName.startIndex, offsetBy: 0)
let firstLetter = String(friend.firstName[index]).uppercased()
if groupedNames[firstLetter] != nil {
//array already exists, just append
groupedNames[firstLetter]?.append(friend)
} else {
//no array for that letter key - init array and store the letter in the groupedLetters array
groupedNames[firstLetter] = [friend]
groupedLetters.append(firstLetter)
}
}
}
Creating a Dictionary structure with Characters as keys & values as Array of User will be more succinct.
The error occurs because you are declaring an empty dictionary, that means you have to add a key / empty array pair if there is no entry for that character.
Consider also to consolidate the question / exclamation marks
class User {
let firstName : String
init(firstName : String) {
self.firstName = firstName
}
}
var friends = [Character: [User]]()
let newFriends = [User(firstName:"foo"), User(firstName:"bar"), User(firstName:"baz")]
for friend in newFriends {
let letter = friend.firstName[friend.firstName.startIndex]
if friends[letter] == nil {
friends[letter] = [User]()
}
friends[letter]!.append(friend)
}

Swift dictionary all containing

Lets say I have dictionaries like below and wanted an array of red dogs. I figured I need to get an array of all the names of the type "dog" using the first dictionary, and then use the name key and the color to search the final dictionary to get ["Polly,"jake"]. I've tried using loops but can't figure out how to iterate through the dictionary.
var pets = ["Polly" : "dog", "Joey" : "goldfish", "Alex" : "goldfish", "jake" : "dog"]
var petcolor = ["Polly" : "red", "Joey" : "black", "Alex" : "yellow", "jake":red"]
The correct solution would seem to be to create a Pet struct (or class) and collate all of this information into a struct and build either an array or dictionary full of these values.
struct Pet {
let name: String
let type: String
let color: String
init(name: String, type: String, color: String) {
self.name = name
self.type = type
self.color = color
}
}
Now, let's build an array of these pets:
var goodPets = [Pet]()
for (petName, petType) in pets {
guard let petColor = petcolor[petName] else {
// Found this pet's type, but couldn't find its color. Can't add it.
continue
}
goodPets.append(Pet(name: petName, type: petType, color: petColor))
}
Now that we've filled out goodPets, pulling out any particular subset of Pets becomes very easy:
let redDogs = goodPets.filter { $0.type == "dog" && $0.color = "red" }
And although this answer looks like a lot of set up & legwork compared to other answers, the major advantage here is that once we build the goodPets array, any way we want to scoop pets out of there ends up being more efficient. And as we increase the number of properties the pets have, this becomes more and more true compared to the other answers.
If you'd rather store our model objects in a dictionary continuing to use the names as the keys, we can do that as well, but the filter looks a little bit stranger.
Building the dictionary looks mostly the same:
var goodPets = [String : Pet]()
for (petName, petType) in pets {
guard let petColor = petcolor[petName] else {
// Found this pet's type, but couldn't find its color. Can't add it.
continue
}
goodPets[petName] = (Pet(name: petName, type: petType, color: petColor))
}
But the filter is slightly different:
let redDogs = goodPets.filter { $0.1.type = "dog" && $0.1.color = "red" }
Note that in both cases, redDogs has the type [Pet], that is, an array of Pet values.
You can iterate through a dictionary like this:
for key in pets.keys() {
if pets[key] == "Dog" {
}
}
Or:
for (name, pet) in pets {
if pet == "Dog" {
}
}
nhgrif is probably correct about structure but, to answer the literal question:
let dogs = Set(pets.filter { $0.1 == "dog" }.map { $0.0 })
let redAnimals = Set(petscolor.filter { $0.1 == "red" }.map { $0.0 })
let redDogs = dogs.intersect(redAnimals)
Each filter is a block that operates on a (key, value) tuple, testing the value and ultimately creating a dictionary with only the matching (key, value) pairs. Each map then converts that filtered dictionary into an array by discarding the values and just keeping the keys.
Each array is turned into a set to support the intersect operation. The intersect then determines the intersection of the two results.

How to remove duplicated objects from NSArray?

I have NSArray() which is include names but there's duplicated names how can i remove them ?
After parse query adding the objects to the NSArray and its duplicated
var names = NSArray()
let query = PFQuery(className: "test")
query.whereKey("receivers", equalTo: PFUser.currentUser()!.username!)
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil {
self.names = objects!
let set = NSSet(array: self.names as [AnyObject])
print(objects!.count)
// count is 4
// database looks like this (justin , kevin , kevin , joe)
If your names are strings you could create NSSet from array and it will have only different names.
let names = ["John", "Marry", "Bill", "John"]
println(names)
let set = NSSet(array: names)
println(set.allObjects)
prints:
"[John, Marry, Bill, John]"
"[Bill, John, Marry]"
Update #1
With new information in question (code fragment) it may look like this
var set = Set<String>()
for test in objects as [Test] {
set.insert(test.sender)
}
self.names = Array(set)
To expand on John's answer, an NSSet will, by definition, only contain a single copy of each object that hashes to be equal. So, a common pattern is to convert the array to a set and back.
This will work for any object type that has a reasonable implementation of -hash and -isEqual:. As John shows, String is one such object.
You could also do it with pure Swift:
let arrayWithDuplicates = [ "x", "y", "x", "x" ]
let arrayWithUniques = Array(Set(arrayWithDuplicates)) // => [ "y", "x" ]
But, it looks like you're already working with NSArray, so I think the John's example is more applicable.
Also, as my example shows, be aware that the order of the final array is not guaranteed to be in the same order as your original. If you want that, I think you can use NSOrderedSet instead of NSSet.
Here is a more complicated way to approach this that works. You could just run through a loop of the array and create a new one from the original. For example:
var check = 0
let originalArray:NSMutableArray = ["x", "y", "x", "z", "y", "z"]
let newArray: NSMutableArray = []
println(originalArray)
for var int = 0; int<originalArray.count; ++int{
let itemToBeAdded: AnyObject = originalArray.objectAtIndex(int)
for var int = 0; int<newArray.count; ++int{
if (newArray == ""){
break;
}
else if ((newArray.objectAtIndex(int) as! String) != itemToBeAdded as! String){
}
else if ((newArray.objectAtIndex(int) as! String) == itemToBeAdded as! String){
check = 1
break
}
}
if (check == 0){
newArray.addObject(itemToBeAdded)
}
}
Basically I set a check var = 0. for every object in the originalArray, it loops through the newArray to see if it already exists and if it does the check var gets set to 1 and the object does not get added twice.