Matching an exact key in SwiftyJson - ios

I'm really having trouble with determining a specific value returned in SwiftyJson; hopefully, someone can help explain this to me.
I want to see if there is a match between a predetermined word, "apple" to any of the words received from the JSON responses.
If there is a match then a message is displayed, and the user either chooses to progress to the next level or the user returns to the home screen.
If there is no match then a message is displayed, and the user must either continue playing or cancel playing.
I would like to do this for multiple words across different levels of the game.
Level one: match "apple" to any of the received JSON responses.
Level two: match "computer" to any of the received JSON responses.
Level three: match "telephone" or "phone" or "iPhone" or "Android" or any or all of the above to any of the received JSON responses.
So, basically, I can get all of the JSON responses, but I'm having a hard time finding out how to set up to determine if there is a specific, predefined JSON response returned.
I have looked everywhere for weeks with another post but to no avail :(
JSON RESPONSES
{
"responses" : [
{
"labelAnnotations" : [
{
"mid" : "\/m\/01m2v",
"score" : 0.9245476,
"description" : "computer keyboard"
},
{
"mid" : "\/m\/01c648",
"score" : 0.7945268,
"description" : "laptop"
},
{
"mid" : "\/m\/01mfj",
"score" : 0.74227184,
"description" : "computer hardware"
},
{
"mid" : "\/m\/0541p",
"score" : 0.7062791,
"description" : "multimedia"
},
{
"mid" : "\/m\/07c1v",
"score" : 0.7039645,
"description" : "technology"
},
{
"mid" : "\/m\/03gq5hm",
"score" : 0.69323385,
"description" : "font"
},
{
"mid" : "\/m\/0bs7_0t",
"score" : 0.6724673,
"description" : "electronic device"
},
{
"mid" : "\/m\/01vdm0",
"score" : 0.66489816,
"description" : "electronic keyboard"
},
{
"mid" : "\/m\/0121tl",
"score" : 0.60392517,
"description" : "electronic instrument"
},
{
"mid" : "\/m\/0h8n5_7",
"score" : 0.5834592,
"description" : "laptop replacement keyboard"
}
]
}
]
}
CODE TO SHOW ALL JSON RESPONSES
// Use SwiftyJSON to parse results
let json = JSON(data: dataToParse)
let errorObj: JSON = json["error"]
// Parse the response
print(json)
let responses: JSON = json["responses"][0]
// Get label annotations
let labelAnnotations: JSON = responses["labelAnnotations"]
let numLabels: Int = labelAnnotations.count
var labels: Array<String> = []
if numLabels > 0 {
var labelResultsText:String = "Labels found: "
for index in 0..<numLabels {
let label = labelAnnotations[index]["description"].stringValue
labels.append(label)
}
for label in labels {
// if it's not the last item add a comma
if labels[labels.count - 1] != label {
labelResultsText += "\(label), "
} else {
labelResultsText += "\(label)"
}
}
self.labelResults.text = labelResultsText
} else {
self.labelResults.text = "No labels found"
}
EDIT
I'm apparently not able to answer my own question, I'll post an edit since I think it's a better solution but #pierce's was pretty decent for a single word, not many; it just wasn't applicable for a game setting application.
So, I created a new NSObject, created a
static var _words: [[String]] = [
["apple", "computer", "beer"]]
then
func checkAnnotations(annotations: [Annotation]) -> Bool {
var isMatched = false
let searchWords = self.words
for searchWord in searchWords {
for annotation in annotations {
if searchWord == annotation.descriptionString {
isMatched = true
break
}
}
if isMatched {
break
}
}
return isMatched
}
then created a function to handle the level state,
and finally compared that to the JSON response in the View Controller and advanced level if matched
// Get JSON key value
let labelAnnotations = responses["labelAnnotations"].arrayValue
let annotationObjects: [Annotation] = labelAnnotations.flatMap({ annotationDictionary in
if let mid = annotationDictionary["mid"].string,
let score = annotationDictionary["score"].double,
let description = annotationDictionary["description"].string {
let annotation = Annotation(mid: mid, score: score, descriptionString: description)
return annotation
}
return nil
})
//print(annotationObjects)
let searchString = LevelState.shared.words[0]
print("Level \(LevelState.shared.level), looking for: \(searchString)")
var isMatched = LevelState.shared.checkAnnotations(annotations: annotationObjects)
if isMatched {
LevelState.shared.advance()
}
let alertTitle = isMatched ? "Congrats! You got \(searchString)" : "Keep looking for \(searchString)"
//let translationResult = "Translated: \(levelDescription) to \(translatedText)"
let alertController = UIAlertController(title: alertTitle, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
self.prepareForNewLevel()
})
}

First thing - I really don't understand why you want to append a comma at the end of each description. I really thing that's unnecessary. Are you confused about separating elements in an array? Because the actual Strings don't require that, it's only if you're manually writing out the elements of an array (i.e. let array = ["a", "b", "c"]).
So then say you setup a property for this labels array, which is an array of Strings.
var labels: Array<String> = []
Once you've gone through and appended all the description values from your JSON, you can then manipulate it.
if numLabels > 0 {
for index in 0..<numLabels {
let label = labelAnnotations[index]["description"].stringValue
labels.append(label)
}
}
Now you could create a method that would return a filtered array of Strings based on some user entered word:
func findMatches(_ userEntry: String) -> [String] {
return labels.filter { $0.contains(userEntry) }
}
Now you could use the above method to handle some sort of user entry, like say you had the text from a UITextField named textField:
// Return the filtered matches based on the textField text (unless nil)
let matches = findMatches(textField.text ?? "")
// Print the number of matches, and also show the matches
print("Found \(matches.count) matches to user input\r\(matches)")
Now if you had labels which held ["a", "aa", "ba", "b", "c", "apple"], and ran the above code where the userEntry was just the letter "a", you'd see this print out in the console window:
Found 4 matches to user input
["a", "aa", "ba", "apple"]
EDIT - You can use the findMatches method above for what you're trying to do with pre-determined words to match. I'm not sure what you're trying to do exactly, but there are a couple different ways. First, say you had an array of pre-determined words you wanted to check as an array:
let words = ["word", "verb", "noun", "adverb"]
Then you could loop through that and check each one
for word in words {
let matches = findMatches(word)
if matches.count > 0 {
print("Found \(matches.count) matches to \(word)\r\(matches)")
} else {
// Do whatever you want when there are no matches
print("No matches found")
}
}
If you want to just check for a specific word, and have a specific response you could setup a method like so:
func checkWord(word: String, noMatchResponse: String) {
let matches = findMatches(word)
if matches.count > 0 {
print("Found \(matches.count) matches to \(word)\r\(matches)")
} else {
// Do whatever with the no match response
print(noMatchResponse)
}
}
There are so many ways you could implement this. You could also use a switch statement, and then use different case statements for each pre-determined word. It's really up to you and how you want to design your game.

Related

How to organise data when populating UICollectionView with images using Firebase

I'm new to Swift, and coding in general, and have been working on a project where I'd like to create a UICollectionView populated with images from Firebase.
Each section of the UICollectionView would be a category, and each category would contain images related to that category. Each UICollectionView belongs to a parent, and I need to keep track of which parent has which categories, and which images are in each category.
To track the parents, categories, and images, I've set up the Firebase database in the following way (with bowl being the parent, the names of fruit as categories, and the keys are references to image data stored elsewhere in the database):
"bowl" : {
"apple" : {
"-LOM1R4EH9nszjJp0Va5" : true,
"-LOM1aRZT2XCE-6fvLBK" : true,
"-LOM1hSTmRY6wGrWMvIo" : true,
"-LOM1xnvKE6lc7fizomh" : true
},
"banana" : {
"-LOLmQWLXXyiCUwDBwID" : true
},
"pear" : {
"-LOLHakW-EtqevCeHfzl" : true,
"-LOM2DBGGuX5VQLmBz46" : true
},
"orange" : {
"-LOM26_pm6lbJ1D6hVPB" : true
}
}
The image data section of the database looks as follows:
"image" : {
"fruit" : {
"-LOLHakW-EtqevCeHfzl" : {
"description" : "round orange",
"imageURL" : "https://firebasestorage.googleapis.com/1/image1"
},
"-LOLmQWLXXyiCUwDBwID" : {
"description" : "big banana",
"imageURL" : "https://firebasestorage.googleapis.com/1/image2"
},
"-LOM1R4EH9nszjJp0Va5" : {
"description" : "small apple",
"imageURL" : "https://firebasestorage.googleapis.com/1/image3"
}
}
}
The approach I have been attempting to take is to create a dictionary with the image keys in it, then iterate through the image keys to grab the image data associated with each key (such as the imageURL), and then use the imageURL to download the images and populate the UICollectionView.
I've created a struct, as follows to transform the image data:
struct FruitPicture {
let imageURL: String
let description: String
init(imageURL: String, description: String) {
self.imageURL = imageURL
self.description = description
}
init?(snapshot: DataSnapshot) {
guard
let value = snapshot.value as? [String: AnyObject],
let imageURL = value["imageURL"] as? String,
let description = value["description"] as? String else {
return nil
}
self.imageURL = imageURL
self.description = description
}
func toAnyObject() -> Any {
return [
"imageURL": imageURL,
"description": description
]
}
}
I've been able to gather the imageURLs and populate a UICollectionView but it doesn't include the category details, and so far has involved a lot of manipulation of the data via snapshots, dictionaries, arrays, arrays of dictionaries, and so on, from one configuration to another and back again, and I've now become stuck and confused.
I've started looking at using multiple structs and nesting one within the other, like so, but I'm muddled on it all and am spending hours getting nowhere:
struct Picture {
var url: URL
var image: UIImage?
}
struct PictureCategory {
var name: String
var pictures: [Picture]
}
I was hoping for some advice, or roadmap, or details of how you would approach this, or some sample code, or anything to point me in the right direction. Thanks.
Edit to add more info
Thank you Iraniya your reply was very helpful and helped me consider things in a different way, I really appreciate it.
Taking your advice I've written the following which looks up a bowling creates a snapshot of the image meta data within (e.g the fruit and keys associated with that fruit) then uses those keys to create a snapshot of the image data (e.g key, imageURL, description). I then transform both snapshots into dictionaries, and return the dictionaries to the method which called it:
// GET DATA
static func getPicData(forKey bowlKey: String, completion: #escaping ([String : [Any]], [String : [FruitPicture]]) -> Void) {
var imageMetaDict: [String : [Any]] = [:]
var imageDataDict: [String : [FruitPicture]] = [:]
// DEFINE DATABASE TARGET
let ref = Database.database().reference().child("meta").child("bowl").child(bowlKey).child("fruit")
// GET DATA INTO SNAPSHOT AND TRANSFORM INTO DICTIONARY
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dict = snapshot.value as? [String:[String:Any]] else {
return completion([:],[:])
}
// DEFINE DISPATCH GROUP
let dispatchGroup = DispatchGroup()
// ITERATAE THROUGH DICTIONARY
for (categoryObject, fruitData) in dict {
// CREATE ARRAY TO STORE ITEMS
var itemArray = [String]()
// ITERATE THROUGH ITEMS IN FRUIT DATA
for item in fruitData {
// APPEND ITEM.KEY TO ITEM ARRAY
itemArray.append(item.key)
// ENTER DISPATCH GROUP
dispatchGroup.enter()
// USE ITEM.KEY TO GATHER IMAGE DATA
Service.viewPicData(forKey: item.key) { (fruitItem) in
if let fruitItem = fruitItem {
imageDataDict[item.key] = [fruitItem]
}
// EXIT DISPATCH GROUP
dispatchGroup.leave()
}
}
// STORE ARRAY IN DICTIONARY UNDER FRUIT CATEGORY KEY
imageMetaDict[categoryObject] = itemArray
}
// RETURN COMPLETION
dispatchGroup.notify(queue: .main, execute: {
completion(imageMetaDict, imageDataDict)
})
})
}
Each dictionary looks similar to the following:
imageMetaDict
[
"apple": ["-LOM1R4EH9nszjJp0Va5", "-LOM1xnvKE6lc7fizomh", "-LOM1hSTmRY6wGrWMvIo", "-LOM1aRZT2XCE-6fvLBK"],
"pear": ["-LOLHakW-EtqevCeHfzl", "-LOM2DBGGuX5VQLmBz46"],
"banana": ["-LOLmQWLXXyiCUwDBwID"],
"orange": ["-LOM26_pm6lbJ1D6hVPB"]
]
imageDataDict
[
"-LOM26_pm6lbJ1D6hVPB": [myApp.FruitPicture(imageURL: "https://firebasestorage.googleapis.com/1/image1", description: "pear 1")],
"-LOM2DBGGuX5VQLmBz46": [myApp.FruitPicture(imageURL: "https://firebasestorage.googleapis.com/1/image2", description: "banana 1")],
"-LOLmQWLXXyiCUwDBwID": [myApp.FruitPicture(imageURL: "https://firebasestorage.googleapis.com/1/image3", description: "apple 1")]
]
Is this on the right track with what you were suggesting?
From what I understand the next steps are:
Create an array of fruit.keys sorted alphabetically
Use the fruit.keys to get image.keys from 'imageMetaDict'
Use those image.keys to look up the image data (imageURL, etc) in 'imageDataDict'
Transform all of this data into a new FruitDict which contains fruitCategory -> [fruitObject]
Is this similar to what you were suggesting? I'm happy to hear any further pointers, code or suggestions you have, you've really helped me so far!
To store images Create imageDict hash-map(dictionary) with the key you getting from firebase key in image->>fruits eg: "-LOLHakW-EtqevCeHfzl" with value you are getting or the stuct you already create, now when populating fruits-->apple get key from your bowl array or dict and then use that same key to get the image from imagesDict dict(hashmap you just create earlier
now while storing data in firebase make sure each image has unique keys and store that same key in your bowl-->apple->image that way it will be fast and easy to manage as image data and fruits data are mapped using key you get while storing new image :-) if you like the solution I can explain in more dept :-) #HappyCoding
Example
firebase node
"bowl" : {
"apple" : {
"-LOLHakW-EtqevCeHfzl" : true,
"--LOLmQWLXXyiCUwDBwID" : false,
}
}
"image" : {
"fruit" : {
"-LOLHakW-EtqevCeHfzl" : {
"description" : "round orange",
"imageURL" : "https://firebasestorage.googleapis.com/1/image1"
},
"-LOLmQWLXXyiCUwDBwID" : {
"description" : "big banana",
"imageURL" : "https://firebasestorage.googleapis.com/1/image2"
}
}
}
ImageDict
"-LOLHakW-EtqevCeHfzl":{
"description" : "round orange",
"imageURL" : "https://firebasestorage.googleapis.com/1/image1"
},
"-LOLmQWLXXyiCUwDBwID" : {
"description" : "big banana",
"imageURL" : "https://firebasestorage.googleapis.com/1/image2"
}
or
{"-LOLHakW-EtqevCeHfzl":imageStruct1,
"-LOLmQWLXXyiCUwDBwID" :imageStruct2}
now to show image while populating apple
var keys = boul["apple"].allKeys;
if(boul["apple"][keys[0]]){ //value is true show image
var imageUrl = imageDict[keys[0]["imageURL"]; //if using dict
//or
var image = imageDict[key[0]].imageURL //if using struct
}
Now to store parent, categories and there image details
create a Dict called fruitsDict or whatever with dict in side of another dict making key as fruite name eg: "apple":{apples Details like image price etc} but if you only interested in storing images just create list of images which have true value eg: "apple":[key1, key2...]; (keys you get from imageDict.
Now based on your requirement like
Show all category!! then create all category from fruitDict use that as datasource
and use imageDict and fruitsDict for details
Show only specific category like based on seasonal fruits then crate list of those fruits and show those based on imageDict and fruiteDict
HappyCoding :-)

Filter and sort swift array of dictionaries

I have the following array:
let parent = [ ["Step":"S 3", "Desc":"Do third step" ],
["Step":"S 1", "Desc":"Do first step" ],
["Step":"S 2", "Desc":"Do second step" ],
["Step":"P 1", "Desc":"Some other thing" ] ]
How do I filter and sort the array(using filter and sort functions) in the least possible steps so that I get the following output string or label --
1.Do first step
2.Do second step
3.Do third step
I'd suggest filtering, sorting, enumerating, mapping, and joining:
let results = parent
.filter { $0["Step"]?.first == "S" }
.sorted { $0["Step"]!.compare($1["Step"]!, options: .numeric) == .orderedAscending }
.enumerated()
.map { (index, value) in "\(index + 1). \(value["Desc"]!)" }
.joined(separator: "\n")
A key consideration is the use of .numeric comparison, so that "S 10" shows up after "S 9", not between "S 1" and "S 2". You don't want to do simple string comparison if you're embedding a numeric value in your string.
I also threw in that enumeration because if you remove an item, you probably want to make sure that the numbers in your list don't skip over a value just because of the particular encoding inside your Step strings.
Unrelated, a dictionary is a poor model for something like this. I'd suggest a custom type:
struct Task {
enum TaskType {
case step
case process // or whatever "P" is supposed to stand for
}
let sequence: Int
let type: TaskType
let taskDescription: String
}
let parent = [Task(sequence: 3, type: .step, taskDescription: "Do third step"),
Task(sequence: 1, type: .step, taskDescription: "Do first step"),
Task(sequence: 2, type: .step, taskDescription: "Do second step"),
Task(sequence: 3, type: .process, taskDescription: "Some other thing")]
let results = parent
.filter { $0.type == .step }
.sorted { $0.sequence < $1.sequence }
.map { "\($0.sequence). \($0.taskDescription)" }
.joined(separator: "\n")
Described answer:
First, you would need to filter the array to get only steps; Based on the posted parent array it seems that the valid step should contain a key as "Step" and a value formatted as "S #", so it could get filtered it as:
let filtered = parent.filter { (currentDict) -> Bool in
// get the value for key "Step"
guard let value = currentDict["Step"] else {
return false
}
// check ifthe value matches the "S #"
let trimmingBySpace = value.components(separatedBy: " ")
if trimmingBySpace.count != 2 || trimmingBySpace[0] != "S" || Int(trimmingBySpace[1]) == nil {
return false
}
return true
}
so far would get:
[["Step": "S 3", "Desc": "Do third step"],
["Step": "S 1", "Desc": "Do first step"],
["Step": "S 2", "Desc": "Do second step"]]
Second, you would sort the filtered array by the value of "Step" key:
let sorted = filtered.sorted { $0["Step"]! < $1["Step"]! }
and you should get:
[["Step": "S 1", "Desc": "Do first step"],
["Step": "S 2", "Desc": "Do second step"],
["Step": "S 3", "Desc": "Do third step"]]
Finally, you would map the sorted array to get the description values:
let descriptions = sorted.map { $0["Desc"] ?? "" }
descriptions should be:
["Do first step", "Do second step", "Do third step"]
All in one step:
let result = parent.filter { (currentDict) -> Bool in
// get the value for key "Step"
guard let value = currentDict["Step"] else {
return false
}
// check ifthe value matches the "S #"
let trimmingBySpace = value.components(separatedBy: " ")
if trimmingBySpace.count != 2 || trimmingBySpace[0] != "S" || Int(trimmingBySpace[1]) == nil {
return false
}
return true
}.sorted {
$0["Step"]! < $1["Step"]!
}.map {
$0["Desc"] ?? ""
}
print(result) // ["Do first step", "Do second step", "Do third step"]
func sortStep(step1:[String:String], step2:[String:String]) -> Bool {
guard let s1 = step1["Desc"], let s2 = step2["Desc"] else {
return false
}
return s1 < s2
}
let orderedStep = parent.sorted { sortStep(step1:$0, step2:$1) }
print("\(orderedStep)")
let sorted = parent.filter({ ($0["Step"]?.hasPrefix("S"))! }).sorted { $0["Step"]! < $1["Step"]!}
var prefix = 0
let strings = sorted.reduce("") { (partial, next) -> String in
prefix += 1
return partial + "\(prefix)." + next["Desc"]! + "\n"
}
I've used forced unwrapping assuming that your structure will remain as mentioned. Feel free to add any checks as required.
parent.filter { $0["Step"]!.contains("S") }.sorted { $0["Step"]! < $1["Step"]!
}.map { print($0["Desc"]!) }

Firebase queryOrderedByChild() method not giving sorted data

My database structure is some thing like this:
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
"score": 4,
},
"ghopper": { ... },
"eclarke": { ... }
}
}
I am trying to retrieve top 20 scores in descending order.
let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToLast(20)
queryRef.observeSingleEventOfType(.Value, withBlock: { (querySnapShot) in
print(querySnapShot.value)
})
i am trying to get output like
score": 4
score": 3
score": 2
or
score": 2
score": 3
score": 4
or
2
3
4
Please let me know how to solve this.
When you request the children in a specific order, the resulting snapshot will contain both the data that matches the query and information about the order in which you requested them.
But when you request the .value of the snapshot, the keys+data are converted to a Dictionary<String,AnyObject>. Since a dictionary does not have an extra place to put the information about the order, that information is lost when converting to a dictionary.
The solution is to not convert to a dictionary prematurely and instead loop over the snapshot:
queryRef.observeSingleEventOfType(.Value, withBlock: { (querySnapShot) in
for childSnapshot in querySnapShot.children {
print(childSnapshot.value)
}
})
You can also listen to the .ChildAdded event, instead of .Value, in which case the children will arrive in the correct value:
queryRef.observeSingleEventOfType(.ChildAdded, withBlock: { (childSnapshot) in
print(childSnapshot.value)
})
Update
I just added this JSON to my database:
{
"users" : {
"alovelace" : {
"name" : "Ada Lovelace",
"score" : 4
},
"eclarke" : {
"name" : "Emily Clarke",
"score" : 5
},
"ghopper" : {
"name" : "Grace Hopper",
"score" : 2
}
}
}
And then ran this code:
let queryRef = ref.child("users").queryOrderedByChild("score").queryLimitedToLast(20);
queryRef.observeEventType(.ChildAdded) { (snapshot) in
print(snapshot.key)
}
The output is:
ghopper
alovelace
eclarke
Which is the users in ascending order of score.
Update to add more on getting the scores in descending order
The above code gets the 20 highest scores in ascending order. There is no API call to return themthem in descending score.
But reversing 20 items client side is no performance concern, you just need to write the code for it. See for example this answer.
If you really are stuck on reversing them client side, you can add an inverted score. See this answer for an example of that.
Use method observeEventType instead of observeSingleEventOfType.
Also, make FIRDataEventType to ChildAdded.
Last, If you want Top 20 items, use queryLimitedToFirst instead of queryLimitedToLast.
{
"users" : {
"alovelace" : {
"name" : "Ada Lovelace",
"score" : 4
},
"eclarke" : {
"name" : "Emily Clarke",
"score" : 5
},
"ghopper" : {
"name" : "Grace Hopper",
"score" : 2
}
}
}
For the dataset above
let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
print("key: \(snapshot.key), value: \(snapshot.value)")
})
key: ghopper, value: Optional({
name = Grace Hopper;
score = 2;
})
key: alovelace, value: Optional({
name = Ada Lovelace;
score = 4;
})
key: eclarke, value: Optional({
name = Emily Clarke;
score = 5;
})
Snapshot will returns the contents as native types.
Data types returned:
NSDictionary
NSArray
NSNumber (also includes booleans)
NSString
So, you can get your scores this way.
let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let scores = snapshot.value as? NSDictionary {
print(scores["score"])
}
})
Optional(2)
Optional(4)
Optional(5)
Moreover, the default of realtime database return everything in ascending order.
If you want descending order, you can make some tricks(4:40) in your database.

Remove a key from a dictionary for a given value

How can I remove a key from a dictionary where the value is X? I need a dictionary.removeKeyForValue(...) function.
I'd like to optimise the following code. I have a text which is associated with a certain category and a dictionary that associates all keywords to categories. Although my text is already categorised I'd like check whether it should fall into a different category.
let text = "he said hello and then ran away" // This is taken from the "activity" category
// Dictionary associating keywords to categories
let categoryRules = ["hi" : "greeting", "hello" : "greeting", "jogging" : "activity", "joy" : "feeling"]
let keywords = Array(categoryRules.keys)
// Make out of text an Array of words.
let textWordArray = text.lowercaseString.characters.split{$0 == " "}.map(String.init)
// I SHOULDN'T HAVE TO GO THROUGH THE KEYS ASSOCIATED WITH "ACTIVITY" BECAUSE TEXT IS ALREADY IN IT.
for keyword in keywords {
// If text contains the rule
if let index = textWordArray.indexOf(keyword) {
// Get the associated category
if let category = categoryRules[keyword] {
print("The text should fall into the category of \(category)")
break
}
}
}
To remove all keys from a dictionary with a certain value, you can use a for loop with where clause to select the keys to remove and then assign nil to remove them:
var categoryRules = ["hi" : "greeting", "hello" : "greeting", "jogging" : "activity", "joy" : "feeling"]
for (key, value) in categoryRules where value == "greeting" {
categoryRules[key] = nil
}
print(categoryRules) // ["jogging": "activity", "joy": "feeling"]
You can add removeKeysForValue by adding an extension to Dictionary that works for values that are Equatable (can be compared with ==):
extension Dictionary where Value: Equatable {
mutating func removeKeysForValue(value: Value) {
for (key, val) in self where val == value {
self[key] = nil
}
}
}
var categoryRules = ["hi" : "greeting", "hello" : "greeting", "jogging" : "activity", "joy" : "feeling"]
categoryRules.removeKeysForValue("greeting")
print(categoryRules) // ["jogging": "activity", "joy": "feeling"]
Here's an alternate solution that I find elegant which uses filters.
var categoryRules = ["hi" : "greeting", "hello" : "greeting", "jogging" : "activity", "joy" : "feeling"]
let keysToRemove = dict.keys.filter { dict[$0]! == "greeting" }
for key in keysToRemove {
dict.removeValueForKey(key)
}
// categoryRules = ["jogging": "activity", "joy": "feeling"]
keysToRemove will have "hi" and "hello" because they matched the given filter of having the value "greeting".
Edit:
OP mentioned that he wanted a dictionary.removeKeyForValue() function. You can create an extension with the above code to increase readability and avoid code duplication if you plan on doing this action often.
Example:
extension Dictionary {
mutating func removeKeysForValue(value: NSObject) {
let keysToRemove = self.keys.filter { self[$0]! as! NSObject == value }
for key in keysToRemove {
self.removeValueForKey(key)
}
}
}
var dict = ["hi" : "greeting", "hello" : "greeting", "jogging" : "activity", "joy" : "feeling"]
dict.removeKeysForValue("greeting") // dict = ["jogging": "activity", "joy": "feeling"]
Since it will take O(n) time to remove keys from the dictionary (where n is the size of the dictionary) it might be a better idea to create a list of dictionaries (instead of just one.) Each dictionary would exclude the necessary elements. So when looking up values out of the dictionary you just have to choose the correct dictionary.
This will have better performance then removing keys from the dictionary at runtime. Especially when doing lookups multiple times in loops.

Firebase in Swift nested query not working properly

I have a JSON structure like the following:
{
"groups" : {
"-KAv867tzVgIghmr15CM" : {
"author" : "ruben",
"name" : "Item A"
},
"-KAv87nqLEG1Jtc04Ebn" : {
"author" : "ruben",
"name" : "Item B"
},
"-KAv88yZe8KTfkjAE7In" : {
"author" : "ruben",
"name" : "Item C"
}
},
"users" : {
"rsenov : {
"avatar" : "guest",
"email" : "ruben#ruben.com",
"groups" : {
"-KAv867tzVgIghmr15CM" : "true",
"-KAv87nqLEG1Jtc04Ebn" : "true",
"-KAv88yZe8KTfkjAE7In" : "true"
}
}
}
}
Every user has the element "groups" with a childByAutoId() key. Then I have the list of all the groups that exists in the app.
Every time that I run the app, I get the current user logged url reference, and I get the list of the groups of that user (in this case, the logged in user is "rsenov" that has 3 groups).
For every group that this user belongs to, I iterate through the groups url reference, looking for getting the information of that 3 groups.
I do this like this:
func loadTable() {
self.groups = []
var counter = 0
self.meses = []
var tempItems = [String]()
DataService.dataService.CURRENT_USER_GROUPS_REF.observeEventType(.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
tempItems = []
for snap in snapshots {
DataService.dataService.GROUPS_REF.childByAppendingPath(snap.key).queryOrderedByChild("name").observeEventType(.Value, withBlock: { snapshot in
if let postDictionary = snapshot.value as? Dictionary<String, AnyObject> {
tempItems.append(snapshot.value.objectForKey("name") as! String)
let key = snapshot.key
let group = Group(key: key, dictionary: postDictionary)
self.groups.insert(group, atIndex: 0)
}
counter++
if (counter == snapshots.count) {
self.meses = tempItems
self.miTabla.reloadData()
}
})
}
}
})
}
I think this is not a good idea of iterating in that way. For example, if there is a change of some child in the GROUPS_REF url, the code only runs in that nested code, and since it doesn't have the "snap.key" value got from the for loop, it doesn't work.
Which is the best way to do a good query in this case?
Phew, that took some time to write. Mostly because I don't iOS/Swift a lot:
let ref = Firebase(url: "https://stackoverflow.firebaseio.com/35514497")
let CURRENT_USER_GROUPS_REF = ref.childByAppendingPath("users/rsenov/groups")
let GROUPS_REF = ref.childByAppendingPath("groups")
var counter: UInt = 0
var groupNames = [String]()
CURRENT_USER_GROUPS_REF.observeEventType(.Value, withBlock: { groupKeys in
for groupKey in groupKeys.children {
print("Loading group \(groupKey.key)")
GROUPS_REF.childByAppendingPath(groupKey.key).observeSingleEventOfType(.Value, withBlock: { snapshot in
print(snapshot.value)
if (snapshot.childSnapshotForPath("name").exists()) {
groupNames.append(snapshot.value.objectForKey("name") as! String)
}
counter++
if (counter == groupKeys.childrenCount) {
print(groupNames)
}
})
}
})
By the way, this is how you create a minimal, complete verifiable example. The code has no external dependencies (such as Group and DataService in your code) and only contains what's relevant to the answer.
The important bits:
I used observeSingleEventOfType to get each group, since I don't want to get more callbacks if a group changes
I use snapshot.childSnapshotForPath("name").exists() to check if your group has a name. You probably want to either ensure they all have names or add them to the list with some other property in the real app.
Frank's answer is on-point. I wanted to throw in an alternative that may or may not work for your situation as it requires a slight alteration to the database.
groups
gid_0
author: "ruben"
name: "Item A"
users
uid_0: true
gid_1
author: "ruben"
name: "Item B"
users
uid_1: true
gid_2
author: "ruben"
name: "Item C"
users
uid_0: true
And then some ObjC Code for a Deep Query
Firebase *ref = [self.myRootRef childByAppendingPath:#"groups"];
FQuery *query1 = [ref queryOrderedByChild:#"users/uid_0"];
FQuery *query2 = [query1 queryEqualToValue:#"true"];
[query2 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
NSLog(#"key: %# value: %#", snapshot.key, snapshot.value);
}];
This code does a deep query on the /groups for all groups that have a /users/uid_0 = true. In this case it returns gid_0 and gid_2
It eliminates the need for iterations and multiple calls to the database.
Adding a /users/ node to each group with a list of the uid's may offer some additional flexibility.
Just a thought.

Resources