SoFunction
Updated on 2025-03-10

A summary of changes brought by Swift 3.0

I won’t say much nonsense, I will just post code to everyone.

 var string = "Hello-Swift"
//Get the character corresponding to the subscript after a subscript char="e"//swift2.2
//var char = string[()]
//swift3.0
var char = string[(after: startIndex)]
//Get the character corresponding to the previous subscript of a subscript char2 = "t"//swift2.2
//var char2 = string[()]
//swift3.0
var char2 = string[(before: )]
//Get a substring in a string through range Hello//swift2.2
//var subString = string[startIndex...(4)]
//swift3.0
var subString = string[startIndex...(startIndex, offsetBy: 4)]
//swift2.2
//var subString2 = string[(-5)...()]
//swift3.0
var subString2 = string[(endIndex, offsetBy: -5)..<endIndex]
//Get the range of a child string in the parent string//swift2.2
//var range = ("Hello")
//swift3.0
var range = (of: "Hello")
//Add string operation At this time string = "Hello-Swift! Hello-World"//swift2.2
//(" Hello-World")
//swift3.0
(" Hello-World")
//Insert a character at the specified position At this time string = "Hello-Swift!~ Hello-World"//swift2.2
//("~", atIndex: (12))
//swift3.0
("~", at: (, offsetBy: 12))
//Insert a set of characters at the specified position At this time string = "Hello-Swift!~~~ Hello-World"//swift2.2
//(["~","~","~"], at: (12))
//swift3.0
(contentsOf: ["~","~","~"], at: (, offsetBy: 12))
//Replace a string in the specified range At this time string = "Hi-Swift!~~~ Hello-World"//swift2.2
//(...(4), with: "Hi")
//swift3.0
(...(, offsetBy: 4), with: "Hi")
//Delete a character at the specified location At this time string = "Hi-Swift!~~~ Hello-Worl"//swift2.2
//(())
//swift3.0
(at: (before:))
//Delete characters in the specified range At this time string = "Swift!~~~~ Hello-Worl"//swift2.2
//(...(2))
//swift3.0
(...(, offsetBy: 2))
var string2 = "My name is Jaki"
//Convert all to uppercase//swift2.2
//string2 = 
//swift3.0
string2 = ()
//Convert all to lowercase//swift2.2
//string2 = 
//swift3.0
string2 = ()

Need to be careful,In Swift 3.0, the Range structure is divided into two types, Range and ClosedRange, which are used to describe the left-closed and right-closed intervals and the closed intervals, respectively, corresponding to the operators: 0..<10 and 0...10.

As can be seen from the above example code, many method naming in the String type has been simplified in Swift style. One of the biggest changes is the change of the index index. The two Index subscript movement methods were removed, and the index() method of the String type was used to perform the subscript movement operation, making programming safer.

The above is a summary of the changes brought by Swift 3.0 that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!