Swift: Using a variable of type int should be possible to use in a `if`

Originator:romaaan.git
Number:rdar://22075800 Date Originated:2015-08-03
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:Always
 
Summary:
Objective-C and other languages resolve an int greater than zero to `true` and values equal or lower than zero to `false`. Using the attached sample code leads to the error message:
Type `int` does not conform to protocol `BooleanType`

Steps to Reproduce:
if 1 {
println("True")
} else {
println("False")
}

Expected Results:
The if structure should print "True"

Actual Results:
Error message: Type `int` does not conform to protocol `BooleanType`

Version:
Xcode 6.4 / Swift 1.2 iOS 8

Comments

From The Swift Programming Language:

"Swift’s type safety prevents non-Boolean values from being substituted for Bool. The following example reports a compile-time error:

let i = 1
if i {
// this example will not compile, and will report an error
} "

This behavior is intentional, and will likely not be changed.

It is, however, relatively simple to extend Int to make it conform to BooleanType, but it's not advisable.

By T.Mokgadi601229 at Aug. 11, 2015, 11:52 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!