A preliminary survey of the changes brought about by Swift 3.0

  • 2020-05-27 07:19:14
  • OfStack

Without further ado, I'm just going to post the code.


 var string = "Hello-Swift"
// After getting some index 1 The character corresponding to the subscript  char="e"
//swift2.2
//var char = string[startIndex.successor()]
//swift3.0
var char = string[string.index(after: startIndex)]
// Before getting some index 1 The character corresponding to the subscript  char2 = "t"
//swift2.2
//var char2 = string[endIndex.predecessor()]
//swift3.0
var char2 = string[string.index(before: string.endIndex)]
// Gets a string by scope 1 substring  Hello
//swift2.2
//var subString = string[startIndex...startIndex.advancedBy(4)]
//swift3.0
var subString = string[startIndex...string.index(startIndex, offsetBy: 4)]
//swift2.2
//var subString2 = string[endIndex.advancedBy(-5)...endIndex.predecessor()]
//swift3.0
var subString2 = string[string.index(endIndex, offsetBy: -5)..<endIndex]
// Gets the scope of a child string in the parent string 
//swift2.2
//var range = string.rangeOfString("Hello")
//swift3.0
var range = string.range(of: "Hello")
// Append string operation   At this time string = "Hello-Swift! Hello-World"
//swift2.2
//string.appendContentsOf(" Hello-World")
//swift3.0
string.append(" Hello-World")
// Inserts at the specified location 1 A character   At this time string = "Hello-Swift!~ Hello-World"
//swift2.2
//string.insert("~", atIndex: string.startIndex.advancedBy(12))
//swift3.0
string.insert("~", at: string.index(string.startIndex, offsetBy: 12))
// Inserts at the specified location 1 Set of characters   At this time string = "Hello-Swift!~~~~ Hello-World"
//swift2.2
//string.insertContentsOf(["~","~","~"], at: string.startIndex.advancedBy(12))
//swift3.0
string.insert(contentsOf: ["~","~","~"], at: string.index(string.startIndex, offsetBy: 12))
// Replace in the specified range 1 A string   At this time string = "Hi-Swift!~~~~ Hello-World"
//swift2.2
//string.replaceRange(string.startIndex...string.startIndex.advancedBy(4), with: "Hi")
//swift3.0
string.replaceSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 4), with: "Hi")
// Delete at the specified location 1 A character   At this time string = "Hi-Swift!~~~~ Hello-Worl"
//swift2.2
//string.removeAtIndex(string.endIndex.predecessor())
//swift3.0
string.remove(at: string.index(before:string.endIndex))
// Deletes the specified range of characters   At this time string = "Swift!~~~~ Hello-Worl"
//swift2.2
//string.removeRange(string.startIndex...string.startIndex.advancedBy(2))
//swift3.0
string.removeSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 2))
var string2 = "My name is Jaki"
// All uppercase 
//swift2.2
//string2 = string2.uppercaseString
//swift3.0
string2 = string2.uppercased()
// All lowercase 
//swift2.2
//string2 = string2.lowercaseString
//swift3.0
string2 = string2.lowercased()

Note that in Swift3.0 the Range structure is divided into two types, Range and ClosedRange, which are used to describe the left closed right open interval and the closed interval respectively, corresponding to the operator 0.. < 10 and 0... 10.

As can be seen from the sample code above, many method names of String type have been simplified in Swift style. One point of significant change is the change of subscript index. Two subscript moving methods of Index type have been removed, and index() method of String type is used to move subscript, which makes programming safer.


Related articles: