A summary of the issues that may be encountered when migrating Swift3 to Swift4

  • 2020-06-19 11:48:38
  • OfStack

preface

WWDC 2017 brought many surprises. Swift 4 is also coming to us with Xcode 9 beta, and many powerful new features are well worth looking forward to using in official projects. This article will give you a detailed introduction of the problems encountered in the migration of Swift3 to Swift4. Let's start with the detailed introduction

The questions are as follows:

1.The use of Swift 3@objc inference Swift 4 mode is deprecated

Choose Target - > Build Settings, search Swift3, in ES30en3@objc Inference, change On to Off or Defalut.

2. subString method is abandoned

In Swift3, if we want to intercept the string, we usually use:


var ctime = "2017-09-28 12:11:32.43234"
ctime = ctime.substring(to: ctime.index(ctime.startIndex, offsetBy: 19)) 

I have to say how painful this statement was when it was first written... Not only difficult to understand, but also in Xcode8 when typing, inexplicable will all the code highlight disappeared, and then prompt Report Bug.

Finally in Swift4 to its modification, although personal feeling is still a little strange ~, but at least easy to use!!


var ctime = "2017-09-28 12:11:32.43234"
let endIndex = ctime.index(ctime.startIndex, offsetBy: 19)
ctime = String(ctime[ctime.startIndex ..< endIndex])
[

Note, however, that SubString USES the memory of the original string. The official recommendation is to use it for a short period of time. If you want to keep it for a long time, you need to switch to String.

]

To make it easier to use, we can extend Sting by writing 1 Extension:


extension String {
 subscript (start: Int, end: Int) -> String? {
  if start > count || start < 0 || start > end {
   return nil
  }
  let begin = self.index(self.startIndex, offsetBy: start)
  var terminal: Index
  if end >= count {
   terminal = self.index(self.startIndex, offsetBy: count)
  } else {
   terminal = self.index(self.startIndex, offsetBy: end)
  }
  let str = self[begin ..< terminal]
  return String(str)
 }
}

3. The Swift4 treatment has not been applied to some third bank

Remove the library from Profile pod update or pod install Check Target - > Build Settings- > Linking- > In Other Linker Flags, is the corresponding framework removed

conclusion


Related articles: