Error : Command failed due to signal : Segmentation fault: 11

11k Views Asked by At

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
        }
    })
}
4

There are 4 best solutions below

0
Anthony Kong On BEST ANSWER

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)

        }

    }
})
3
Ryan Westcott On

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{
    //...
}
1
Sherif Aziz On

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.

1
A.J. Hernandez On

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