2015-01-20 3 views
0

NSDictionary에서 값을 가져 오려고하는데, 여기서 치명적인 오류가있는 EXC_BAD_INSTRUCTION은 두 곳입니다. 그리고 나는이 문제없이있는 NSDictionary에서 값을 얻는 방법에 흥미있어이NSDictionary에서 값을 가져 오는 동안 선택적 값의 래핑을 수행하는 동안 예기치 않게 nil이 발견되었습니다.

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>"; 
}; 
} 
+0

의 중복 가능성 [치명적인 오류 : 예기치 않게 옵션 값을 풀기 동안 nil을 발견] (http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optption-values) –

+1

나는 그렇게 생각하지 않는다. –

+0

그 다음에 다른 10 가지 질문을 살펴 본다. 오른쪽의 '관련'항목 –

답변

0

이 "item.value[latitude] == nil || item.value[latitude] as NSNull == NSNull()는"과잉이다, 그것을 수행하는 기존의 방법과 내 생각 값을 NSNull에 대해 확인하기 위해 랩핑을 해제하면 Catch-22가 생성되어 크래시가 발생합니다. "할 경우"에 관계없이, 스위프트 선택적 항목과 훨씬 더 나은 방법이있다 : 당신은 경우에만-다음, 어쨌든 가장 간단한 사용되어야 ? : 짧은 버전으로이 작업을 수행 할 수

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

.

+0

답변 주셔서 감사합니다. 나는 과잉이지만, 모든 것을하려고 노력했다. 심지어 오류 : 치명적인 오류 : 예기치 않게 찾을 수 없음 선택 값을 래핑하는 동안 –

+1

해결되었습니다. 예외를 피하기 위해 값을 검사하는 코드를 더 추가합니다. 전체 코드는 입니다.'if let checkLongitude : AnyObject? = item.value [경도] { 만약 longNull = checkedLongitude로하면? NSNull { gr.longitude = 0.0 한다} else { gr.longitude = checkedLongitude 블룸 } 한다} else { gr.longitude = 0.0 }' –

0

두 맞춤법 오류, 열쇠에 사전에 다른 하나가 있습니다 다음

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