2014-11-30 2 views
-2

FBLoginCutomUISample을 목표 C에서 swift로 변환하려고합니다. 지금까지 모든 것이 잘 작동합니다. 나는 다음과 같은 몇 가지 솔루션 시도NSDictionnary를 목표 C에서 swift로 변환하십시오.

//Get more error information from the error 
NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"]; 

// Show the user an error message 
alertTitle = @"Something went wrong"; 
alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]]; 
[self showMessage:alertText withTitle:alertTitle]; 

: 난 그냥이 시점에 박히면서

if let info = error.userInfo { 
    let errorInformation = info["com.facebook.sdk:ParsedJSONResponseKey"]["body"]["error"] 
    let msg = errorInformation["message"] 
    println("errormessage: \(msg)") 
} 

를하지만 때마다 나에게 같은 오류를 제공합니다 : '(NSObject의, AnyObject)'는이 없습니다 'subscript'라는 이름의 멤버. 그것은 풀리지 않는 문제인 것처럼 보이지만 어떻게 해결해야할지 모르겠습니다. 감사합니다

[UPDATE 해결 방법] 드디어 여기에 작업 코드를 업데이트 할 수 있습니다 아래의 대답에서
:

if let info = error.userInfo{ 
    if let dict1 = info["com.facebook.sdk:ParsedJSONResponseKey"] as? NSDictionary { 
     if let dict2 = dict1["body"] as? NSDictionary { 
      if let errorInformation = dict2["error"] as? NSDictionary { 
       if let msg:AnyObject = errorInformation["message"] { 
        println("errormessage: \(msg)") 
       } 
      } 
     } 
    } 
} 
+0

배열/사전 참조. ..]'는 객체에 대한 "subscript"메소드를 호출합니다. 컴파일러는 가지고있는 객체의 종류를 알지 못합니다. –

답변

2

스위프트의 모든 사전 조회 풀어해야하는 옵션을 반환합니다. 또한 조건부 캐스트 as?을 사용하여 예상되는 유형을 Swift에 알려야 할 수도 있습니다. ?과 함께 각 사전 조회 후에 선택적 체인을 사용하여 여러 사전 액세스를 함께 연결할 수 있습니다.

이 시도 :

if let info = error.userInfo { 
    if let errorInformation = info["com.facebook.sdk:ParsedJSONResponseKey"]?["body"]?["error"] as? NSDictionary { 
     if let msg = errorInformation["message"] { 
      println("errormessage: \(msg)") 
     } 
    } 
} 

를 문제가 해결되지 않으면, 당신은 당신이 예상하는 각 단계에서 스위프트에게 가질 수있는 NSDictionary : [`를 통해

if let info = error.userInfo { 
    if let dict1 = info["com.facebook.sdk:ParsedJSONResponseKey"] as? NSDictionary { 
     if let dict2 = dict1["body"] as? NSDictionary { 
      if let errorInformation = dict2["error"] as? NSDictionary { 
       if let msg = errorInformation["message"] { 
        println("errormessage: \(msg)") 
       } 
      } 
     } 
    } 
} 
+0

답장을 보내 주셔서 감사합니다. 두 코드를 모두 시험해 보았는데 새로운 오류가 나타납니다. " 'NSDictionary'는 '[NSObject : AnyObject]'의 하위 유형이 아닙니다." 나는 왜 그것이 객관적인 C에서 NSDictionary 일 수 있었는지와 스위프트에있을 수없는 이유를 얻지 못한다. (죄송합니다. 언어로 시작합니다.) – grll

+0

'as? 첫 번째 예제에서는 두 곳 모두에서 'NSDictionary'를 사용합니다. 스위프트는 이미이 경우 사전이라고 알고 있습니다. – vacawama

+0

사실 두 번째 예제에서는 첫 번째 줄에서만 "as NSDictionary"를 제거했습니다. Swift는 이미 error.userInfo 유형을 알고 있지만이 사전 내부의 유형은 알지 못합니다. 고마워. – grll