2016-06-30 4 views
2

Swift 3 환경으로 이동하려는 JSON 파일을 읽는 코드가 있습니다. 여기있다 :Swift to Swift 3 코드 변환 : JSON 파일 읽기

do { 
    let dictionary = try NSJSONSerialization.JSONObjectWithData(dataOK, options: NSJSONReadingOptions()) as AnyObject! 
     dictionaryOK = (dictionary as! NSDictionary as? Dictionary <String, AnyObject>)! 
    } 
catch { 
    print("Level file '\(filename)' is not valid JSON: \(error)") 
    return nil 
} 

엑스 코드이 산출 (일부 객체와 클래스 이름이 변경되었습니다로) 일부 오류를 수정 제안을 제공합니다

do { 
    //Problem here: parameters don't match overrides 
    let dictionary = try JSONSerialization.jsonObject(dataOK as Data, options: JSONSerialization.ReadingOptions()) as AnyObject! 
    dictionaryOK = (dictionary as! NSDictionary as? Dictionary <String, AnyObject>)! 
} 
catch { 
    print("Level file '\(filename)' is not valid JSON: \(error)") 
    return nil 
} 

합니다 (jsonObjects 대체 일치하지 않는 그것은 말한다). jsonObjects에 대한 매개 변수가 다음과 같아야한다는 설명서를 확인했습니다.

class func jsonObject(with data: Data, 
      options opt: JSONSerialization.ReadingOptions = []) throws -> AnyObject 

무엇이 잘못 되었나요?

답변

9

귀하의 스위프트 2 코드는 매우 상세하게 시작됩니다.

이 시도 :

do { 
    if let dictionaryOK = try JSONSerialization.jsonObject(with: dataOK, options: []) as? [String: AnyObject] { 
     // parse JSON 
    } 
} catch { 
    print(error) 
}