"'init' is deprecated" warning after Swift4 convert

With Swift 4.2 and Swift 5, you can choose one of the 5 following solutions in order to solve your problem.


#1. Using NSNumber's intValue property

import Foundation

let integer = NSNumber(value: false).intValue
print(integer) // prints 0

#2. Using type casting

import Foundation

let integer = NSNumber(value: false) as? Int
print(String(describing: integer)) // prints Optional(0)

#3. Using Int's init(exactly:) initializer

import 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)

#4. Using Int's init(truncating:) initializer

import Foundation

let integer = Int(truncating: false)
print(integer) // prints 0

#5. Using control flow

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

Tags:

Swift

Swift4