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
基础教程推荐
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
