2016-10-11 18 views
0

私は最近、Swift 3/XCode 8に更新され、コードの一部は乾いたワイヤーになりました。私はいくつかの構文の変更が行われていることを読んだが、私はこの1つの権利を得ることはできません。 「結合条件付きの初期化子がない「どれ 誰かができるオプションの種類を持っている必要があります。と言う文を「場合は聞かせて」JSONの解析とオプションのSwift 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) 
     } 
    } 
} 
} 

私は2番目にエラーが発生します。 私はツイッターへの要求を行い、バックJSONを取得しますこれがある理由を説明?

+1

エラーがこの行に由来しているように見えます。もしあれば、どんな=試してもいいですか?...また、2番目の '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]

ですそれはconコンパイラを融合します。

疑問符なし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"]などの参照項目を使用することはできません。また、あなたはlet json = jsonを使ってjsonを再宣言しています。これは、あなたのlet immage = 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 
    { 
    } 
} 

コンパイラエラーはありません。