将 Int 转换为 Bool [英] Converting Int to Bool

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

问题描述

在 Swift 2.x 中我相信我可以做到:

让数字 = 1让结果=布尔(数字)print(result)//打印出来:true

但自从 Swift 3 以来,我一直无法做到这一点,它给了我错误:

<块引用>

无法使用参数列表为Bool"类型调用初始化程序输入(整数)"

目前我正在使用扩展将 Int 转换为 Bool 但我想知道是否没有内置选项来执行此操作.

解决方案

没有,并且从来没有明确的内置选项用于将 Int 转换为 Bool,请参阅Bool 的语言参考了解详情.p>

然而,仍然存在一个 初始化器由 NSNumber.不同之处在于 Swift 数字类型和 NSNumber 之间的隐式桥接已在 Swift 3 中删除(以前允许 IntBool> 初始化).您仍然可以通过 NSNumber 初始化程序通过显式执行从 IntNSNumber 的转换来访问它:

让数字 = 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)//false

In 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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