我正在使用一个APP,它将请求发送到Web服务器并以JSON字符串形式接收答案。在这个方法中,我将它转换为字典,检查用户是否有效,然后我想将用户ID分配给全局变量。 我的问题是我从JSON字符串获得的用户ID不会被分配给'userID'。
var userID: String!
// authenticate the user by an email and a password
func authtenticateUser(email: String, password: String) {
execHttpRequest(token: "/api/v1/authenticate/", parameter: ["email": "\(email)", "password": "\(password)"], method: "POST", completion: {
returnString in
do {
let content = try JSONSerialization.jsonObject(with: returnString, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject]
if let success: Bool = content?["success"] as? Bool, success {
print("successfully authenticated")
if let id: String = content?["id"] as? String {
self.userID = id
}
}
} catch {
print("Error: \(error)")
}
})
}