Preface
Swift 4 has been released recently, full update logPlease click here >>:, I roughly read the official update log. Because xcode9 supports both Swift4 and Swift3.2, my heart is not so painful when upgrading Swift4. When converting to Swift4, the error is concentrated on adding @objc. If the project is a project that coexists between OC and Swift, then adding @objc will make you add to doubting your life.
The following mainly introduces some problems encountered during the update process. I won’t say much below, let’s take a look at the detailed introduction together.
@objc
1. In Swift4, projects that coexist with OC and Swift no longer provide Swift's Public properties and methods to OC without any brains, unless the methods and properties in Swift are marked with @objc, which reduces the generation of some code and reduces the size of the package. After building, we are pure swift projects, so most of them need to add @objc in front of the method called by the selector.
2. Our project is a Swift project, so the build saving is quickly modified and completed. After running, it will be Crashed as scheduled. This is because our JSON to Model uses the OC version of Mantle and SwiftJSON to parse, so we need to add @objc to all Model properties, otherwise Mantle will be empty when obtaining the class attribute type. Seeing that at this time, I was heartbroken and refused to upgrade Swift4, so we chose to abandon Mantle and use Codable.
Codable( Official Documentation)
1. In OC, JSON can only be converted to object types, and in Codable, JSON can be converted to regular types. What we encounter more is changing the NSNumber in the data model to Int or Double, because NSNumber is an OC type that does not comply with Codable.
2. Sometimes we will add some fields that JSON string does not have in the Model for logical processing, similar
struct A: Codable{ var a: Int? var isSelect: Bool = false }
Because "isSelect" is for logical processing that the corresponding fields cannot be found in the JSON string, an error of data loss will be reported, and we only need to change "isSelect" to optional.
stuct A: Codable { var a: Int? var isSelect: Bool? }
In this upgrade, we mainly change the problem of adding @objc to OC calls Swift and changing the data parsing to Codable. There are also some errors because of the problem of using Mantle to JSON string. As long as you use JSONEncode, there will be additional string length warnings to determine the use of characters to delete it.
It's annoying to use generics normally
I define a method with generics
private func getData<T>(a: T) { }
Call directly
<A>(a)
Appears because you don't know the type of generic
Cannot explicitly specialize a generic function
Error, if type not specified
(a)
The following error will appear
Generic parameter 'Element' could not be inferred
Only one force rotation can be performed when used
(a as A)
It is to let Xcode infer its type instead of telling him what type this generic is, which is very annoying.
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.