iOS Swift Alamofire JSON响应将Bool值返回为“1”或“0”

时间:2017-06-12 06:16:52

标签: ios json swift3 alamofire boolean-operations

在alamofire回复中,我返回了一些0值。但它返回1true而不是falsetrue。如何将Alamofire JSON响应设置为falseAlamofire.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)") } } 。这是我尝试过的代码:

{{1}}

2 个答案:

答案 0 :(得分:0)

您可以尝试输入类型

  let JSON = response.result.value! as Bool

答案 1 :(得分:0)

适用于Swift 3

您可以将其转换为bool,如下所示:

要将特定字符串值转换为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
    }
}
}

您可以按如下方式使用

var mySting = "0"
var myBool = myString.toBool()