Reputation: 1775
I have a son file which is an array of dictionary entries. A sample entry looks like
{"title":"Limits - Evaluating Limits by Graphing mini lecture","chapter":"2","section":"1","path":"chap2/","fileName":"2.1-1-Limits-Evaluating_Limits_by_Graphing_mini_lecture.mp4"}
I want to filter the entries out by chapter and save them into an array. So I have the following lines of code:
currentChapter = "2"//just an example
let parsedObject:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parseError)
if let videoList = parsedObject as? [NSDictionary] {
let videoListForChapter = videoList.filter {$0["chapter"] == self.currentChapter}
}
Upvotes: 0
Views: 69
Reputation: 9687
You are using .filter on NSDictionary, it should work as you would expect if you were using just Dictionary.
Try changing the code to the following
let parsedObject = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parseError) as [Dictionary<String: String>]
Then remove the if statement and you are left with
let videoListForChapter = parsedObject.filter {$0["chapter"] == self.currentChapter}
Here is some code you can copy and paste into a playground based on your sample entry:
var array : [[String:String]] = [["title":"Limits - Evaluating Limits by Graphing mini lecture",
"chapter":"2",
"section":"1",
"path":"chap2/",
"fileName":"2.1-1-Limits-Evaluating_Limits_by_Graphing_mini_lecture.mp4"],
["title":"test Title",
"chapter":"1",
"section":"1",
"path":"chap2/",
"fileName":"2.1-1-Limits-Evaluating_Limits_by_Graphing_mini_lecture.mp4"]]
array[0]
let videoListForChapter = array.filter {$0["chapter"] == "2"}
videoListForChapter
Upvotes: 1