The company recently upgraded the project to Swift4.2, which modification points, share with the friends who also need to upgrade.

1. Expression splitting

Previously, there was no limit to the complexity and length of the expression assignment. After the upgrade, too complex an expression will report an error. Note that this is an error, not a warning, which means that you must not use complex expressions.

It could have been written as follows:

But in swift4.2 sentence will report error, investigate down to find self. Parameters! . HeadLineNumber * fontHeight. HeadFontHeight such calculations as part of the expression will be an error. However, if you change this statement to a direct expression like 2 * 3, it will compile properly. So if you want to write an assignment like this, if you want to use a value, you need to break it up.

2.UIAccessibilityTraintsThe assignment value of

Since the project supports UIAccessibility, the previous view’s accessibilityTraits attribute assignment can be written like this:

self.onJobImageView.accessibilityTraits |= UIAccessibilityTraits.selected
Copy the code

But in swift4.2 sentences would be an error, because UIAccessibilityTraits attribute type change, the original is UInt64, now is a struct, cannot do | = operation, direct to:

let accessibilityTraits = self.onJobImageView.accessibilityTraits.rawValue | UIAccessibilityTraits.selected.rawValue
self.onJobImageView.accessibilityTraits = UIAccessibilityTraits(rawValue: accessibilityTraits)
Copy the code

3. @objcThe addition of

After upgrading to Swift4.2, all OC variables and functions accessing Swift need to be added with @objc, otherwise warning will be displayed

4.String(describing:)

After upgrading exceptString(not includingString!String?Describing data in format format using String(describing:)

Repair warning as prompted

Use an interpolator

And of course you can be lazy and write something else to avoid seeing this repetition

[juejin. Im/post / 684490…].

5. NSArrayType processing of

In Swift,NSArray needs to define a specific type. If it is converted from OC, it needs to be converted to [Any] when calling. Before Swift4.2, the system automatically prompt us to add the strong XXX as! [Any], but in Swift4.2 if there is a strong transfer warning, let us know

The solution is as follows: Fix: XXX as? [Any]

6.UIImagePickerControllerthedelegateTo deal with

Before the upgrade

if let image = info[UIImagePickerControllerOriginalImage] {}Copy the code

After the upgrade

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : // Local variable inserted by Swift 4.2 migrator. let info = convertFromUIImagePickerControllerInfoKeyDictionary(info) if let image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] { } } // Helper function Inserted by Swift migrator. 4.2 fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary (_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] { return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, Value)})} // Helper function inserted by Swift 4.2 migrator.fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String { return input.rawValue }Copy the code

The info [convertFromUIImagePickerControllerInfoKey (UIImagePickerController. InfoKey)] doesn’t make any sense. You are specifying an entire enumeration type. InfoKey rather than a specific value, for example:

The info [convertFromUIImagePickerControllerInfoKey (UIImagePickerController. InfoKey. OriginalImage)] it may also can be written as: info[.originalImage] as! UIImage

7. .substring(to: Index)

Compiling on iOS10, use substring(to: will report warning. This function will be deprecated instead of:

let orign: String = "aaabbbbccccc"
let endIndex = orign.index(orign.endIndex, offsetBy: -3)

let sub1 = orign.substring(to: endIndex)  // Is deprecated in swift4.2
let sub = String(orign[..<endIndex])      // Substitution

let sub3 = orign.substring(from: endIndex) // Is deprecated in swift4.2
let sub2 = String(orign[endIndex.])      // Substitution
Copy the code