In Swift3, I previously converted a Bool to an Int using the following method
let _ = Int(NSNumber(value: false))
After converting to Swift4, I'm getting a "'init' is deprecated" warning. How else should this be done?
With Swift 4.2 and Swift 5, you can choose one of the 5 following solutions in order to solve your problem.
NSNumber
's intValue
propertyimport Foundation
let integer = NSNumber(value: false).intValue
print(integer) // prints 0
import Foundation
let integer = NSNumber(value: false) as? Int
print(String(describing: integer)) // prints Optional(0)
Int
's init(exactly:)
initializerimport Foundation
let integer = Int(exactly: NSNumber(value: false))
print(String(describing: integer)) // prints Optional(0)
As an alternative to the previous code, you can use the more concise code below.
import Foundation
let integer = Int(exactly: false)
print(String(describing: integer)) // prints Optional(0)
Int
's init(truncating:)
initializerimport Foundation
let integer = Int(truncating: false)
print(integer) // prints 0
Note that the following sample codes do not require to import Foundation.
Usage #1 (if statement):
let integer: Int
let bool = false
if bool {
integer = 1
} else {
integer = 0
}
print(integer) // prints 0
Usage #2 (ternary operator):
let integer = false ? 1 : 0
print(integer) // prints 0
You can use NSNumber property intValue:
let x = NSNumber(value: false).intValue
You can also use init?(exactly number: NSNumber)
initializer:
let y = Int(exactly: NSNumber(value: false))
or as mentioned in comments by @Hamish the numeric initializer has been renamed to init(truncating:)
let z = Int(truncating: NSNumber(value: false))
or let Xcode implicitly create a NSNumber from it as mentioned by @MartinR
let z = Int(truncating: false)
Another option you have is to extend protocol BinaryInteger (Swift 4) or Integer (Swift3) and create your own non fallible initializer that takes a Bool as parameter and returns the original type using the ternary operator as suggested in comments by @vadian:
extension BinaryInteger {
init(_ bool: Bool) {
self = bool ? 1 : 0
}
}
let a = Int(true) // 1
let b = Int(false) // 0
let c = UInt8(true) // 1
let d = UInt8(false) // 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With