0

I am trying to get values from NSDictionary, but here is two place where is EXC_BAD_INSTRUCTION with fatal error. And I'm interesting in how to get values from NSDictionary without this problems

private func checkResponseResult(responseResult: NSDictionary) {

    // Initialize Group object and [Group] arrays
    println(responseResult)

    for item in responseResult {

        //create object of Group, set attributes, add to Array

        var itemKey = item.key as NSString

        if itemKey.isEqualToString("error") {

            // Error received, user has no groups assigned

            println("Error: \(item.value)")
        } else {

            // Groups values received

            println("Core Data insert / group id: \(item.key)")
            var gr:Group = Group()

            var name = "name"
            var latitude = "latitude"
            var longitude = "longitude"
            var project = "project"
            var radius = "raidus"

            var val = item.value[longitude]
            //return nil
            println(val)
           //return false
           println(val==nil)

            gr.id = itemKey.integerValue
            gr.name = item.value[name] as String
            gr.latitude = item.value[latitude] == nil || item.value[latitude] as NSNull == NSNull() ? 0.0 : item.value[latitude] as NSNumber

           //fatal error: unexpectedly found nil while unwrapping an Optional value
            gr.longitude = item.value[longitude] == nil || item.value[longitude] as NSNull == NSNull() ? 0.0 : item.value[longitude] as NSNumber

            gr.project = item.value[project] as String

            //fatal error: unexpectedly found nil while unwrapping an Optional value
            gr.radius = item.value[radius] == nil || item.value[radius] as NSNull == NSNull() ? 0.0 : item.value[radius] as NSNumber

        }

    }

}  

NSDictionary is here

{
30 =     {
    latitude = "<null>";
    longtitude = "<null>";
    name = mtmb;
    project = "pr_mtmb";
    radius = "<null>";
};
}
Ro Man
  • 48
  • 5
  • possible duplicate of [Fatal error: unexpectedly found nil while unwrapping an Optional values](http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values) – Hot Licks Jan 20 '15 at 16:47
  • Then check out the other ten questions in the "Related" column to your right. – Hot Licks Jan 20 '15 at 17:19
  • Checked. But there is no answer for me. :-( – Ro Man Jan 20 '15 at 17:36
  • You know, I hope, that you can't expect to find a prior question that uses the exact same symbol names that you use. – Hot Licks Jan 20 '15 at 17:40
  • @HotLicks Unwrapping optionals in Swift is a complex topic involved with all different parts of the language. Anyway, the question you linked is dequeing a CollectionView cell and getting a crash when setting the title, this is setting properties on a model object from a dictionary. – Mike Sand Jan 21 '15 at 17:20
  • So you're saying that none of the 10 "related" questions in any way matches your scenario?? – Hot Licks Jan 21 '15 at 17:26

2 Answers2

0

This "item.value[latitude] == nil || item.value[latitude] as NSNull == NSNull() " is overkill, it's the old way of doing it and I think unwrapping the value to check it against NSNull is what's causing the crash, creating a Catch-22. Regardless there's a much better way with Swift optionals, "if let":

if let checkedLongitude = item.value[longitude] {
    gr.longitude = checkedLongitude
} else {
    gr.longitude = 0.0 as NSNumber
}

You can't do this with the ? : short version, which should only be used for the most simple if-then, anyway.

Mike Sand
  • 2,650
  • 12
  • 22
  • Thanks for the answer. I know that its overkill, but i was trying to do everything. Even your method doesn't work. Same error: fatal error: unexpectedly found nil while unwrapping an Optional value – Ro Man Jan 20 '15 at 17:25
  • 1
    Solved. I add some more code to check values to avoid exceptions. Here is full code `if let checkedLongitude: AnyObject? = item.value[longitude]{ if let longNull = checkedLongitude as? NSNull{ gr.longitude = 0.0 } else { gr.longitude = checkedLongitude as Double } } else { gr.longitude = 0.0 }` – Ro Man Jan 21 '15 at 09:59
0

You have two spelling errors, one in your dictionary and another in your keys:

30 =     {
    latitude = "<null>";
    **longtitude** = "<null>";
    name = mtmb;
    project = "pr_mtmb";
    **radius** = "<null>";
};

then

var longitude = "longitude"
var radius = "raidus"
gr.longitude = item.value[longitude] == nil || item.value[longitude] as NSNull == NSNull() ? 0.0 : item.value[longitude] as NSNumber
gr.radius = item.value[radius] == nil || item.value[radius] as NSNull == NSNull() ? 0.0 : item.value[radius] as NSNumber
Mike Sand
  • 2,650
  • 12
  • 22