1

I am trying to get an array from dictionary, but I am getting an error for below line

self.items = self.dataDictionary["geoNames"] as NSArray

Complete code is as below

var dataDictionary: AnyObject!
var items: NSArray!

override func viewDidLoad() {
    super.viewDidLoad()

    var url = NSURL(string: "http://api.geonames.org/countryInfoJSON?username=temp")
    var urlRequest = NSURLRequest(URL: url!)

    NSURLConnection.sendAsynchronousRequest(urlRequest, queue:NSOperationQueue(), completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        if (data.length > 0 && error == nil){
            self.dataDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)
            println(self.dataDictionary)
            self.items = self.dataDictionary["geoNames"] as NSArray
        }
    })
}
Ankita Shah
  • 2,178
  • 3
  • 25
  • 42

4 Answers4

2

A Hypothesis: If the editor is - for some reason - unable to parse through your code and make conclusions about the correctness of your code, it might allow you to compile even if you have syntax errors, which might lead to the error you describe.

I was getting this error because of syntax errors. Namely, I was changing a 1D array to a 2D array, but had forgotten to update some of the places it is initialized.

It seems that the editor was unable to pinpoint exactly where the errors were and when I tried compiling, I got the error you are describing. I suspected something funky was going on with the editor because it was flashing between all-white and colored syntax, and throwing the "An internal error happened" error message at the top of the editor.

So if you have this error, manually double-checking your code or undoing your changes one by one until you get to a stage where you can compile successfully might give you a hint of what's going wrong.

Posting because it might be helpful to someone facing a similar issue.

Sherif Aziz
  • 99
  • 1
  • 3
2

I had the same error with a cocoapod install and noticed my Foundation.framework stop being recognized. Take a look at my answer here on this thread which solved my problem. Hope this helps someone.

In the event you don't want to visit the link, simply run

sudo gem install cocoapods

to update the pods if you suspect your outdated cocoa pods are giving you trouble with the frameworks

Community
  • 1
  • 1
A.J. Hernandez
  • 969
  • 11
  • 13
  • lol ok. In that case, don't visit the link and run "sudo gem install cocoa pods" in your terminal if you suspect cocoa pods is giving you an issue with frameworks – A.J. Hernandez Jun 15 '16 at 04:48
1

There are a few problems with your code:

  1. Your code does not even compile. Segfault is coming from the compiler, not at runtime

  2. You should cast the result from JSONObjectWithData as NSDictionary, not assign to a variable of type AnyObject!

  3. Should use if to check if the casting works

  4. The dictionary key is wrong. It is geonames (all lowercase)

Here is the functional code:

var url = NSURL(string: "http://api.geonames.org/countryInfoJSON?username=temp")
var urlRequest = NSURLRequest(URL: url!)

NSURLConnection.sendAsynchronousRequest(urlRequest, queue:NSOperationQueue(), completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

    if (data.length > 0 && error == nil){
        if let jsonObject = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary {

            let dataDictionary = jsonObject["geonames"]
            println(dataDictionary)

        }

    }
})
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
0

This happened to me because I incorectly created an if statement. Very very simple error, I just missed one key, but caused a world a difference.:

if textBox?.characters.count = 0{
    //...
}

instead I needed to do:

if textBox?.characters.count == 0{
    //...
}
Ryan Westcott
  • 199
  • 3
  • 14
  • This doesn't really answer the question, and doesn't point to an issue in this question... Its more of an aside, and when you have more rep, would be better as a comment – Stuart Siegler Sep 06 '15 at 23:47
  • Something like this could also cause the error for some people. Should I delete it? @Stuart Siegler – Ryan Westcott Sep 06 '15 at 23:50
  • No, just for the future... You'll have the rep in no time, and you'll be able to comment, and edit, etc. We're self moderated, so we help and guide the newer users. – Stuart Siegler Sep 06 '15 at 23:59