2016-10-11 3 views
0

그래서 최근 Swift 3/XCode 8로 업데이트되었고 일부 코드는 건초 선으로 바뀌 었습니다. 나는 약간의 구문 변경이 있었지만이 하나의 권리를 얻을 수없는 것으로 보았습니다. "결합 조건에 대한 초기화하지 '모든 사람이 할 수있는 옵션 유형이 있어야합니다. 말한다 문"을 경우하는하자 "JSON 구문 분석 및 옵션 선택 3

func forLoadStats(completion: (AnyObject?, NSError?) -> Void) 
{ 
    var clientError: NSError? 
    let idString = api.getUserID() 
    let client = TWTRAPIClient() 
    let request = client.urlRequest(withMethod: "GET", url: "https://api.twitter.com/1.1/users/show.json", parameters: ["user_id" : 27446437], error: &clientError) 
    client.sendTwitterRequest(request) 
    { (response, data, connectionError) in 

    if (connectionError == nil) 
    { 
    do { 
     if let json: Any = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [AnyObject] 
      { 
      if let json = json, let immage = json?["profile_image_url_https"] as? String 
       { 
       //Make ProfilePic edges round 
       self.profPic.layer.cornerRadius = 42 
       self.profPic.clipsToBounds = true 

       //let immage = image["profile_image_url_https"] as String 
       let _vImageUrl = immage.replacingOccurrences(of: "_normal", with: "") 
       let urlProfilePic = NSURL(string: _vImageUrl) 
       let urlPP = NSData(contentsOf: urlProfilePic! as URL) 
       self.profPic.image = UIImage(data: urlPP! as Data) 

       let ScrName = json["screen_name"] as! String 
       self.scrNameLabel.text = "@\(ScrName)" 

       //Populate Followers Label.text 
       let flwrVar = json["followers_count"] as! Int 
       self.followerLbl.text = "\(flwrVar)" 

       //Populate Following Label.text 
       let flwngVar = json["friends_count"] as! Int 
       self.followingLbl.text = "\(flwngVar)" 

       //Populate Bio 
       let bio = json["description"] as! String 
       self.bioLabel.text = "\(bio)" 

       //created at date 
       let accountAge = json["created_at"] as! String 

       self.createdLbl.text = "\(accountAge)" 

       let tweetCount = json["statuses_count"] as! Int 
       self.tweetCount.text = "\(tweetCount)" 

       let likes = json["favourites_count"] as! Int 
       self.likesCount.text = "\(likes)" 

       let lists = json["listed_count"] as! Int 
       self.listedCount.text = "\(lists)" 
       } 
      } 
     } 
     catch let error 
     { 
      print(error) 
     } 
    } 
} 
} 

내가 두 번째에 오류가 : 나는 트위터에 요청을하고 다시 JSON을 얻을 이는 이유를 설명?

+1

이 줄에서 오류가 발생하는 것처럼 보입니다. 'if let json : Any = try?'... 또한 두 번째'if let' 행은'let json = json, let immage = json? '이 나에게 보이는 것처럼 보입니다. 그것은 잘못된 IF 비교이므로 그 사람들을보고보십시오 – KSigWyatt

+0

@KSigWyatt하지만 정확히 무엇이 될 수 있습니다. 지금은 아무 것도 생각할 수 없습니다. –

+0

'Any '에 주석을 달고 실제로'[String : Any]'인'[AnyObject]'에 캐스트하는 목적은 무엇입니까 ??? – vadian

답변

2

당신의 JSON 분명히 사전이다, 스위프트 3에서 JSON 사전 당신 때문에 (Any?을 있어야하지만, 실질적으로 무의미하다) 바보 Any 주석에 의해 오류를 발생 [String:Any]

입니다 그것은 컴파일러를 융합합니다. 당신이 do 블록을 사용하는 경우

은 물음표없이 try하지만 옵션 바인딩을 사용 : 구문에 문제가 몇 가지 있습니다

... 
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any] { 
    if let immage = json["profile_image_url_https"] as? String { ... 
+0

고마워요! 그것은 모았다. 아직 스위프트 구문에 익숙해 져서 아직 많은 강이 교차되어 있다고 생각합니다. –

0

. [AnyObject]는 json [ "profile_image_url_https"]과 같은 참조 항목을 사용하지 않습니다. 또한 json = json을 통해 json을 재 선언하고 있습니다. 그러면 json = [json?] [ "profile_image_url_https"] 호출에서이 옵션을 사용할 수 없으므로 문제가됩니다. 이 구조체에는 컴파일러 오류가 없습니다.

if let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any] 
{ 
    if let json = json, let immage = json["profile_image_url_https"] as? String 
    { 
    } 
} 

이것은 컴파일러 오류가 없습니다.