【发布时间】:2016-04-22 19:00:12
【问题描述】:
我正在使用 alamofire 从 web api 获取 HTTP 响应。只是想问一下如何在不使用快速对象映射器框架的情况下解析响应中的 JSON 值并将其传递给 authenticatedUser。我对 loadUserData 有一些问题,因为响应返回响应类型并且我的函数与 NSArray 一起工作我如何转换为使用响应
func GetUserCredential(username:String,password:String)->UserModel
{
let authenticatedUser = UserModel()
let user = username
let password = password
let credential = NSURLCredential(user: user, password: password, persistence: .ForSession)
Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
.authenticate(usingCredential: credential)
.responseJSON { response in
return self.loadUserData(response);
}
return authenticatedUser;
}
func loadUserData(response: Response<AnyObject, NSError>) -> UserModel
{
var userObj = UserModel()
for resp as? Response<AnyObject, NSError> in response
{
let user:String = (resp["user"] as! String)
let isAuthenticated:Bool = (resp["authenticated"] as! Bool)
let isManager:Bool = true
userObj = UserModel(username:user,isAuthenticated:isAuthenticated,isManager:isManager)
}
return userObj
}
【问题讨论】:
-
根据报错信息
response["SUCCESS"]返回Response<AnyObject, NSError>而不是NSArray -
@vadian 我试图更改为 as?
没用 -
您是否在文档中查找了 API 的正确签名?
-
你能看到更新吗
-
你对我下面的回答有什么好运气吗?
标签: ios json parsing httpresponse alamofire