我試圖在if語句中使用securityCode
變量,但它說它的'未解決的標識符',任何想法爲什麼?Swift 3 - 聲明的變量給我'未解決的標識符錯誤'
繼承人我的代碼:
func loginAction (sender: UIButton!){
guard let url = URL(string: "myurl") else{ return }
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
print(data)
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let securityCode = parseJSON["security"] as? Bool
print("security code bool: \(String(describing: securityCode))")
}
} catch {
print(error)
}
}
}.resume()
if securityCode! == true {
let layout = UICollectionViewFlowLayout()
let mainScreen = MainController(collectionViewLayout: layout)
present(mainScreen, animated: true, completion: nil)
}
}
不要將'securityCode'聲明爲隱式解包。使其成爲可選項或使其成爲非可選項並賦予其初始值。 – rmaddy
@rmaddy你說得對,我更新了我的代碼。謝謝! –
@Moe Abdul-Hameed謝謝!現在正常工作 – Jeamz