在获取变量“ firstName”的内容以显示在当前视图上时遇到问题。
我有一个有两个选项卡的分段视图控制器。我想使用下面的代码片段在标签“ nameLabel”上显示firstName,这是主要的容器视图之一。
linkedinHelper.authorizeSuccess({ (lsToken) -> Void in
print("linkedin token: \(lsToken)")
linkedinHelper.requestURL("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,picture-urls::(original),positions,date-of-birth,phone-numbers,location)?format=json", requestType: LinkedinSwiftRequestGet, success: { (response) -> Void in
let profileDictionary:[String : Any] = response.jsonObject as! [String : Any]
self.linkedinprofile.firstName = profileDictionary["firstName"] as! String
let firstName = profileDictionary["firstName"] as! String
self.nameLabel?.text = firstName
})
}, error: { (error) -> Void in
//Encounter error: error.localizedDescription
}, cancel: { () -> Void in
//User Cancelled!
})
但是,执行此代码后,标签保持为空。我对此进行了很多试验都没有用。但是,当我从第二个容器视图中用标签替换该标签时,当我单击该容器视图时,该标签会显示我需要的内容!
您的帮助将不胜感激!
谢谢
答案 0 :(得分:0)
尝试在主线程中更新nameLabel
。所有UI更新都必须在此进行。
DispatchQueue.main.async {
self.nameLabel?.text = firstName
}
或者也许这是nameLabel
尚不存在的时刻。在此处放置一个断点,看看它是否不是nil
。