"'Init' Is Deprecated" Warning After Swift4 Convert

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

Swift 4: 'init' is deprecated. CountableRange is now Range

You don't need the Range.init. In other words, change:

return self[Range(i ..< i + 1)]

to:

return self[i ..< i + 1]

what replaces init(name:float:) now that it has been deprecated

From the iOS 10 Release Notes:

iOS 10 deprecates several APIs, including:

...

Several SKUniform symbols related to floating point values. Instead, use methods such as initWithName:vectorFloat2: and uniformWithName:matrixFloat2x2:, as appropriate.

For example:

SKUniform(name: "size", 
vectorFloat2:vector2(
Float(self.frame.size.width),
Float(self.frame.size.height)
))

class_copyMethodList returning only init method

You have to expose your methods to Objective C with the @objc attribute.

Like this:

class MyClass: NSObject {
@objc func method1(){
print("Method1")
}

@objc func method2(){
print("Method2")
}
}

Convert Data to DispatchData in Swift 4

Use withUnsafeBytes to get an UnsafeRawPointer to the data bytes,
create an UnsafeRawBufferPointer from that pointer and the count,
and pass it to the DispatchData constructor:

let formattedString = "A string"
let stringData = formattedString.data(using: .utf8)! // Cannot fail!

let data = stringData.withUnsafeBytes {
DispatchData(bytes: UnsafeRawBufferPointer(start: $0, count: stringData.count))
}

Alternatively, create an array from the Strings UTF-8 view and
use withUnsafeBytes to get an UnsafeRawBufferPointer
representing the arrays contiguous element storage:

let formattedString = "A string"
let data = Array(formattedString.utf8).withUnsafeBytes {
DispatchData(bytes: $0)
}

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

You should leave one side empty, hence the name "partial range".

let newStr = str[..<index]

The same stands for partial range from operators, just leave the other side empty:

let newStr = str[index...]

Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:

let newStr = String(str[..<index])

You can read more about the new substrings here.



Related Topics



Leave a reply



Submit