[英]iOS Swift Alamofire JSON Response Returning Bool Values as `1` or `0`
In alamofire response I'm returning some Bool
Values. 在alamofire响应中,我返回了一些Bool
值。 But it return as 0
or 1
not true
or false
. 但是它返回0
或1
而不是true
或false
。 How to make Alamofire JSON response to return it as true
or false
. 如何使Alamofire JSON响应将其返回为true
或false
。 Here is my tried code: 这是我尝试的代码:
Alamofire.request(baseurl, method: .get,encoding: JSONEncoding.default).responseJSON { response in
let statuscode = response.response?.statusCode
switch response.result
{
case .success(_):
if ( statuscode == 200)
{
let JSON = response.result.value!
// I'm having some values like `isUserRegistered` is `true` or `false`. But it return as `0` or `1`.
}
case .failure(let error):
print("Request Failed With Error:\(error)")
}
}
您可以尝试强制类型转换
let JSON = response.result.value! as Bool
For Swift 3 对于Swift 3
You can convert it into bool as followings : 您可以将其转换为bool,如下所示:
To convert your specfic String values to Bool you can use this extension 要将特定的String值转换为Bool,可以使用此扩展名
extension String {
func toBool() -> Bool? {
switch self {
case "True", "true", "yes", "1":
return true
case "False", "false", "no", "0":
return false
default:
return nil
}
}
}
And you can use it as follows 您可以按以下方式使用它
var mySting = "0"
var myBool = myString.toBool()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.