Summary of some of the issues encountered in the swift4 update

  • 2020-06-07 05:23:16
  • OfStack

preface

Recently, Swift 4 was released. For a full update log, click here > > I had a look at the official update log. Because xcode9 supports both Swift4 and Swift3.2, so when upgrading Swift4, my heart did not hurt so much. When converting to Swift4, my mistake was to add @objc.

The following is mainly introduced in the update process encountered in some of the problems, the following words do not say much, to see a detailed introduction.

@objc

1. In Swift4, projects that co-exist with OC and Swift no longer have the brain to provide Public attributes and methods of Swift to OC, unless the methods and attributes in Swift are tagged with @objc, which reduces the generation of 1 bit of code and thus reduces the size of the package. After build we are pure swift projects so most of the time we add @objc before the method that the selector calls.

2. Our project is the project of Swift, so the modification of build was completed soon after it was saved. After the Run was up, the Crash was completed as scheduled. This is because our JSON transfer Model is using OC version of Mantle and SwiftJSON for parsing, so we need to add @objc before all Model attributes, otherwise Mantle is empty when getting the class attribute type. Seeing that my heart was broken and refused to upgrade Swift4, we chose to abandon Mantle and use Codable.

Codable (Official document)

1. In OC, JSON to Model can only be converted to object type; in Codable, JSON is supported to be converted to regular type. More often than not, NSNumber is changed to Int or Double in the data model because NSNumber is the type of OC that does not comply with Codable.

2. Sometimes, we will add 1 field in JSON that JSON string does not have for logical processing, similar


struct A: Codable{
 var a: Int? 
 var isSelect: Bool = false
}

Since "isSelect" is for logical processing and the corresponding field cannot be found in the JSON string, it will report a data loss error, and we just need to change "isSelect" to optional.


stuct A: Codable {
 var a: Int?
 var isSelect: Bool?
}

This upgrade is mainly to fix the problem of OC calling Swift to add @objc and change the data parsing to Codable. There are also some errors due to the problem of using Mantle to convert JSON string. As long as JSONEncode is used, there will be an extra string length.

Using generics is a tedious operation

I define a method with a generic type


private func getData<T>(a: T) {
}

Direct call


self.getData<A>(a)

Occurs because you do not know the type of a generic type


Cannot explicitly specialize a generic function

Error if no type is specified


self.getData(a)

The following error occurs


Generic parameter 'Element' could not be inferred

Can only be used in one strong turn


self.getData(a as A)

It's annoying to have Xcode infer his type instead of you telling him what the generic type is.

conclusion


Related articles: