LockSmith 返回(NSDictionary?, NSError?):
public class func loadDataForUserAccount(userAccount: String, inService service: String = LocksmithDefaultService) -> (NSDictionary?, NSError?)
所以,在你的情况下,dictionary 本身就是Optional:
试试:
var access_token = dictionary?["access_token"] as? String
这里,access_token 是String?,也可以是nil。如果要将其存储到AnyObject 变量中,则必须将其解包。例如,使用"Optional Binding":
if let token = access_token {
// Here, `token` is `String`, while `access_token` is `String?`
self.access_token = token
}
或者更直接:
if let access_token = dictionary?["access_token"] as? String {
// Here, access_token is `String`, not `String?`
self.access_token = token
}
else {
// error handling...
}
顺便说一句,如果你想使用 SwiftyJSON,你应该从 dictionary 创建 JSON 对象。
if let dict = dictionary {
var json = JSON(dict)
var access_token = json["access_token"].stringValue
}