Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Int to Bool

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.

like image 364
Henny Lee Avatar asked Oct 15 '16 14:10

Henny Lee


People also ask

How do you convert a number to boolean?

We convert a Number to Boolean by using the JavaScript Boolean() method. A JavaScript boolean results in one of the two values i.e true or false. However, if one wants to convert a variable that stores integer “0” or “1” into Boolean Value i.e “true” or “false”.

How do you convert int to boolean in Python?

Integers and floating point numbers can be converted to the boolean data type using Python's bool() function. An int, float or complex number set to zero returns False . An integer, float or complex number set to any other number, positive or negative, returns True .

Is bool the same as int?

bool values are convertible to int type, with true converting to 1 and false converting to 0 . This is guaranteed by the language. P.S. C language also has a dedicated boolean type _Bool (macro-aliased as bool ), and its integral conversion rules are essentially the same as in C++.

Is bool an int in C++?

C++ does not really have a boolean type; bool is the same as int. Whenever an integer value is tested to see whether it is true of false, 0 is considered to be false and all other integers are considered be true.


1 Answers

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 
like image 133
dfrib Avatar answered Sep 25 '22 01:09

dfrib