从 NSDictionary 获取值时展开可选值时意外发现 nil
Unexpectedly found nil while unwrapping an optional value while getting values from NSDictionary
我正在尝试从 NSDictionary 获取值,但这里有两个地方 EXC_BAD_INSTRUCTION 出现致命错误。
我对如何在没有这个问题的情况下从 NSDictionary 获取值很感兴趣
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 在这里
{
30 = {
latitude = "<null>";
longtitude = "<null>";
name = mtmb;
project = "pr_mtmb";
radius = "<null>";
};
}
这个“item.value[latitude] == nil || item.value[latitude] as NSNull == NSNull()
”有点矫枉过正,这是老方法了,我认为展开值以根据 NSNull 检查它是导致崩溃的原因,创建了一个 Catch-22。不管 Swift 选项有更好的方法,"if let":
if let checkedLongitude = item.value[longitude] {
gr.longitude = checkedLongitude
} else {
gr.longitude = 0.0 as NSNumber
}
您不能使用 ? :
简短版本执行此操作,它应该只用于最简单的 if-then,无论如何。
您有两个拼写错误,一个在您的字典中,另一个在您的键中:
30 = {
latitude = "<null>";
**longtitude** = "<null>";
name = mtmb;
project = "pr_mtmb";
**radius** = "<null>";
};
然后
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
我正在尝试从 NSDictionary 获取值,但这里有两个地方 EXC_BAD_INSTRUCTION 出现致命错误。 我对如何在没有这个问题的情况下从 NSDictionary 获取值很感兴趣
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 在这里
{
30 = {
latitude = "<null>";
longtitude = "<null>";
name = mtmb;
project = "pr_mtmb";
radius = "<null>";
};
}
这个“item.value[latitude] == nil || item.value[latitude] as NSNull == NSNull()
”有点矫枉过正,这是老方法了,我认为展开值以根据 NSNull 检查它是导致崩溃的原因,创建了一个 Catch-22。不管 Swift 选项有更好的方法,"if let":
if let checkedLongitude = item.value[longitude] {
gr.longitude = checkedLongitude
} else {
gr.longitude = 0.0 as NSNumber
}
您不能使用 ? :
简短版本执行此操作,它应该只用于最简单的 if-then,无论如何。
您有两个拼写错误,一个在您的字典中,另一个在您的键中:
30 = {
latitude = "<null>";
**longtitude** = "<null>";
name = mtmb;
project = "pr_mtmb";
**radius** = "<null>";
};
然后
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