Also ich vor kurzem auf Swift 3/XCode 8 aktualisiert und einige meiner Code ging Heu-Draht. Ich habe gelesen, dass einige Syntaxänderungen vorgenommen wurden, aber ich kann nicht scheinen, dass dies richtig ist. Ich mache eine Anfrage an Twitter und JSON zurück:JSON Parsing & Optionals in 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)
}
}
}
}
Ich erhalte eine Fehlermeldung auf der zweiten Aussage „Wenn wir“, das sagt: „Initialisierung für bedingte Bindung optional Typ nicht haben muss‚Jeder Kann jemand. erklären, warum dies ist?
scheint, als ob der Fehler von dieser Linie herrührt 'wenn json: Any = try? '... Auch die zweite' if let'-Zeile hier lassen Sie json = json, lassen Sie immlage = json? 'Scheint mir wie es ist ein ungültiger 'IF' Vergleich also versuchen Sie, diese – KSigWyatt
@KSigWyatt zu betrachten Aber was genau könnte es sein. Momentan fällt mir nichts ein. –
Was ist der Zweck zu kommentieren 'Any' und Cast zu' [AnyObject] ', die eigentlich' [String: Any] '??? – vadian