Converting Int to Bool(将 Int 转换为 Bool)
问题描述
在 Swift 2.x 中我相信我可以做到:
让数字 = 1让结果=布尔(数字)print(result)//打印出来:true但自从 Swift 3 以来,我一直无法做到这一点,它给了我错误:
<块引用>无法使用参数列表为Bool"类型调用初始化程序输入(整数)"
目前我正在使用扩展将 Int 转换为 Bool 但我想知道是否没有内置选项来执行此操作.
没有,并且从来没有明确的内置选项用于将 Int 转换为 Bool,请参阅Bool 的语言参考了解详情.p>
然而,仍然存在一个 初始化器由 NSNumber代码>.不同之处在于 Swift 数字类型和 > 初始化).您仍然可以通过 NSNumber 之间的隐式桥接已在 Swift 3 中删除(以前允许 IntBoolNSNumber 初始化程序通过显式执行从 Int 到 NSNumber 的转换来访问它:
让数字 = 1让结果 = Bool(作为 NSNumber 的数字)打印(结果)//真正如@Hamish 在下面的评论中所写:如果我们离开初始化器的主题而只关注最终结果(实例化一个 Bool 实例给定一个 Int 实例)我们可以简单地将 != 运算符用于 Int 值(特别是带有签名的运算符 func !=(lhs: Int, rhs:Int) -> Bool),使用 != 运算符方法可以轻松实现的泛化:
让数字 = -1让结果=数字!= 0打印(结果)//真<小时>
就像您自己以及 @JAL 在他的回答中描述的,您可以构建自己的 Bool 通过 Int 初始化程序,但您不妨考虑将其推广到符合 整数协议:
扩展布尔{init<T: 整数>(_ num: T) {self.init(num != 0)}}/* 示例用法 */让 num1: Int8 = -1让 num2: Int = 3让 num3: UInt64 = 0//....让 result1 = Bool(num1)//true让 result2 = Bool(num2)//true让 result3 = Bool(num3)//falseIn Swift 2.x I believe I could do:
let number = 1
let result = Bool(number)
print(result) // prints out: true
But since Swift 3 I've been unable to do this and it gives me the error:
Cannot invoke initialiser for type 'Bool' with an argument list of type '(Int)'
Currently I'm using an extension to convert an Int to a Bool but I was wondering if there isn't a build in option to do this.
No, there is and has never been an explicit built in option for conversion of Int to Bool, see the language reference for Bool for details.
There exists, still, however, an initializer by NSNumber. The difference is that implicit bridging between Swift numeric type and NSNumber has been removed in Swift 3 (which previously allowed what seemed to be explicit Bool by Int initialization). You could still access this by NSNumber initializer by explicitly performing the conversion from Int to NSNumber:
let number = 1
let result = Bool(number as NSNumber)
print(result) // true
As @Hamish writes in his comment below: if we leave the subject of initializers and just focus on the end result (instantiating a Bool instance given the value of an Int instance) we can simply make use of the != operator for Int values (specifically, the operator with signature func !=(lhs: Int, rhs: Int) -> Bool), a generalization easily achievable using the != operator approach:
let number = -1
let result = number != 0
print(result) // true
Much like you yourself as well as @JAL describes in his answer, you could construct your own Bool by Int initializer, but you might as well consider generalizing this for any type conforming to the Integer protocol:
extension Bool {
init<T: Integer>(_ num: T) {
self.init(num != 0)
}
}
/* example usage */
let num1: Int8 = -1
let num2: Int = 3
let num3: UInt64 = 0
// ....
let result1 = Bool(num1) // true
let result2 = Bool(num2) // true
let result3 = Bool(num3) // false
这篇关于将 Int 转换为 Bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 Int 转换为 Bool
- UITextView 内容插图 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
