3

I am beginner in swift 2 and I not find the problem. the error is in this line:

let jsonDictionary try NSJSONSerialization.JSONObjectWithData = (data!, options: NSJSONReadingOptions.MutableContainers) as! Dictionary
func searchItunesFor(searchTerm: String) {
    let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
    let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    let urlPath: String = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
    let url: NSURL = NSURL(string: urlPath)!
    let session = NSURLSession.sharedSession()
    session.dataTaskWithURL(url, completionHandler: {(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        do {
            if let ipString = NSString(data:data!, encoding:NSUTF8StringEncoding){
                let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! Dictionary

                let results: NSArray = jsonDictionary["results"] as NSArray
                dispatch_async(dispatch_get_main_queue(), {
                    self.tableData = results
                    self.appsTableView?.reloadData()
                })
            }
        }catch{
            print("Bad ")
        }
    }).resume()
Tunaki
  • 132,869
  • 46
  • 340
  • 423

3 Answers3

0
let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
  • 2
    Can you explain your answer so other users have a greater benefit from it? – Qbyte Jan 17 '16 at 01:23
  • Although this code may help to solve the problem, it doesn't explain _why_ and/or _how_ it answers the question. Providing this additional context would significantly improve its long-term educational value. Please [edit] your answer to add explanation, including what limitations and assumptions apply. – Toby Speight Sep 29 '16 at 14:22
0

I hope this could help some one even though it's a bit late:

The issue here is that you are trying to cast a JsonObject to a Dictionary instead of a NSDictionary.

So you only need to replace Dictionary with NSDictionary in your code. That should take care of the error.

Swift 2

let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

Swift 3

let jsonDictionary = try JSONSerialization.JSONObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
pableiros
  • 14,932
  • 12
  • 99
  • 105
Fidel
  • 1,173
  • 11
  • 21
0

What's working for me in Swift 3 is to define the dictionary key value types.

if let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary<String, String>{
    // something happens here
} 

Notice the NSDictionary<String, String> at the end of that line.

This is if your object looks like this, of course:

{
    "someProp" : "some value",
    "someOtherProp" : "some other value"
}

If you don't know the value you're getting and it could be Any object use NSDictionary<String, Any>

HotFudgeSunday
  • 1,403
  • 2
  • 23
  • 29