Preface
WWDC 2017 brought a lot of surprises. Swift 4 also came to us with the Xcode 9 beta version, and many powerful new features are worth looking forward to using it in formal projects. This article will introduce you in detail about the problems encountered when migrating Swift3 to Swift4. I won’t say much below, let’s take a look at the detailed introduction together.
The questions are as follows:
use of Swift 3 @objc inference in Swift 4 mode is deprecated.
Select Target->Build Settings, search for Swift3, and in Swift3 @objc Inference, change On to Off or Defalut.
The subString method is abandoned
In Swift3, we usually use:
var ctime = "2017-09-28 12:11:32.43234" ctime = (to: (, offsetBy: 19))
I have to say how painful it was when I first wrote this sentence. . . Not only is it difficult to understand, but when you knock in Xcode8, all the code will be highlighted inexplicably and then prompt for a Report bug.
Finally, it was modified in Swift4. Although I feel it is still a bit strange~, it is at least much easier to use! !
var ctime = "2017-09-28 12:11:32.43234" let endIndex = (, offsetBy: 19) ctime = String(ctime[ ..< endIndex])
But note that SubString uses the original string memory. The official suggestion is to use it for a short time. If you want to maintain it for a long time, you need to switch to a String.
To be more convenient to use, we can write an Extension to extend Sting:
extension String { subscript (start: Int, end: Int) -> String? { if start > count || start < 0 || start > end { return nil } let begin = (, offsetBy: start) var terminal: Index if end >= count { terminal = (, offsetBy: count) } else { terminal = (, offsetBy: end) } let str = self[begin ..< terminal] return String(str) } }
3. Some third-party libraries do not adapt to Swift4 processing methods
- Remove the library from the Profile
- pod update or pod install
- Check whether the corresponding framework is removed in Target->Build Settings->Linking->Other Linker Flags
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.