在Swift 3中将布尔值转换为整数值 [英] Converting Boolean value to Integer value in swift 3

查看:132
本文介绍了在Swift 3中将布尔值转换为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从swift 2转换为swift3.我注意到我无法在swift 3中将布尔值转换为整数值:\.

I was converting from swift 2 to swift 3. I noticed that I cannot convert a boolean value to integer value in swift 3 :\ .

let p1 = ("a" == "a") //true

print(true)           //"true\n"
print(p1)             //"true\n"

Int(true)             //1

Int(p1)               //error

例如,这些语法在swift 2中运行良好.但是在swift 3中,print(p1)会产生错误.

For example these syntaxes worked fine in swift 2. But in swift 3, print(p1) yields an error.

错误是error: cannot invoke initializer for type 'Int' with an argument list of type '((Bool))'

我了解为什么会发生错误.谁能解释这个安全的原因是什么,以及如何在Swift 3中从Bool转换为Int?

I understand why the errors are happening. Can anyone explain what is the reason for this safety and how to convert from Bool to Int in swift 3?

推荐答案

尝试一下,

let p1 = ("a" == "a") //true
print(true)           //"true\n"
print(p1)             //"true\n"

Int(true)             //1

Int(NSNumber(value:p1)) //1

这篇关于在Swift 3中将布尔值转换为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆