swift 3 中来自 alamofire 服务的循环结果
loop result from alamofire service in swift 3
我正在使用 alamofire 和 promiseKit,我想 return 结果为 JSON 这是我的代码:
Services.objServices.ServLogin(auth: auth_dato).then{
data -> Void in
print(data)
SVProgressHUD.dismiss()
UIApplication.shared.endIgnoringInteractionEvents()
}.catch{
error -> Void in
SVProgressHUD.dismiss()
UIApplication.shared.endIgnoringInteractionEvents()
}
func ServLogin(auth: String) -> Promise<String>{
return Promise<String>{
fullfil,reject -> Void in
return Alamofire.request(
Constants.api_url+"login",
headers: ["Authorization":"Basic "+auth]).responseString{
response in
switch(response.result){
case .success(let data):
fullfil(data)
case .failure(let error):
reject(error)
}
}
}
}
现在我得到这个结果:
{
"status": true,
"data": {
"usuarios": [
{
"id": 1,
"username": "user",
"password": "0012",
"nombres": "Diego",
"apellidos": "Moreno",
"estado_user": 1
}
]
}
}
我现在想如何获取用户的状态值和名称?
我正在尝试这个 print(data["status"])
但我得到这个错误:
cannot subscript a value of Type "String" with a index of type
"String"
提前致谢
我建议你使用 https://github.com/SwiftyJSON/SwiftyJSON 库,它使解析 json 变得非常容易。
let json = JSON(data)
let status = json["status"].boolValue
我正在使用 alamofire 和 promiseKit,我想 return 结果为 JSON 这是我的代码:
Services.objServices.ServLogin(auth: auth_dato).then{
data -> Void in
print(data)
SVProgressHUD.dismiss()
UIApplication.shared.endIgnoringInteractionEvents()
}.catch{
error -> Void in
SVProgressHUD.dismiss()
UIApplication.shared.endIgnoringInteractionEvents()
}
func ServLogin(auth: String) -> Promise<String>{
return Promise<String>{
fullfil,reject -> Void in
return Alamofire.request(
Constants.api_url+"login",
headers: ["Authorization":"Basic "+auth]).responseString{
response in
switch(response.result){
case .success(let data):
fullfil(data)
case .failure(let error):
reject(error)
}
}
}
}
现在我得到这个结果:
{
"status": true,
"data": {
"usuarios": [
{
"id": 1,
"username": "user",
"password": "0012",
"nombres": "Diego",
"apellidos": "Moreno",
"estado_user": 1
}
]
}
}
我现在想如何获取用户的状态值和名称?
我正在尝试这个 print(data["status"])
但我得到这个错误:
cannot subscript a value of Type "String" with a index of type "String"
提前致谢
我建议你使用 https://github.com/SwiftyJSON/SwiftyJSON 库,它使解析 json 变得非常容易。
let json = JSON(data)
let status = json["status"].boolValue