Comparison of Swift 3.0 and Swift 2

Posted by spellbinder on Tue, 28 May 2019 00:52:48 +0200



I. API changes in String classes

Apart from the extensive modifications to the API in the Swift version of the Cocoa framework, some of Swift's core libraries have also undergone considerable changes.  
String, the string type in Swift 3.0, is more concise on the method API, and the method associated with subscripts varies greatly, as follows:


var string = "Hello-Swift"
//Gets the character char="e" corresponding to a subscript
//swift2.2
//var char = string[startIndex.successor()]
//swift3.0
var char = string[string.index(after: startIndex)]
//Gets the character char2 = t corresponding to the previous subscript of a subscript
//swift2.2
//var char2 = string[endIndex.predecessor()]
//swift3.0
var char2 = string[string.index(before: string.endIndex)]
//Get a substring Hello in a string by scope
//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 range of a child string in the parent string
//swift2.2
//var range =  string.rangeOfString("Hello")
//swift3.0
var range = string.range(of: "Hello")
//Additional string operation at this time string = Hello-Swift! Hello-World
//swift2.2
//string.appendContentsOf(" Hello-World")
//swift3.0
string.append(" Hello-World")
//When a character is inserted at a specified position, 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))
//Inserting a set of characters at the specified position, 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 a string with a specified range when 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")
//String = Hi-Swift! ~Hello-Worl when a character is deleted at a specified position
//swift2.2
//string.removeAtIndex(string.endIndex.predecessor())
//swift3.0
string.remove(at: string.index(before:string.endIndex))
//Delete the specified range of characters when 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 converted to uppercase
//swift2.2
//string2 = string2.uppercaseString
//swift3.0
string2 = string2.uppercased()
//All converted to lowercase
//swift2.2
//string2 = string2.lowercaseString
//swift3.0
string2 = string2.lowercased()


It should be noted that in Swift 3.0, the Range structure is divided into two types, Range and LosdRange, which are used to describe left-closed and right-open intervals and closed intervals respectively, corresponding to operators 0. <10 and 0. 10.  
As can be seen from the example code above, many method naming in String type simplifies Swift style. One of the major changes is about the change of subscript index, removing two methods of index subscript movement, using the index() method of String type for subscript movement operation, programming is safer.  

Change of Array Array Array

Examples of API s modified in array arrays are as follows:

//Create arrays of a large number of identical elements
//Create an array of 10 String-type elements, each of which is the string "Hello"
//swift2.2
//var array3 = [String](count: 10, repeatedValue: "Hello")
//swift3.0
var array3 = [String](repeating: "Hello", count: 10)
//Create an array of 10 Int-type elements, each of which is 1
//swift2.2
//var array4 = Array(count: 10, repeatedValue: 1)
//swift3.0
var array4 = Array(repeating: 1, count: 10)

var array = [1,2,3,4,5,6,7,8,9]
//Append a set of elements to an array
//swift2.2
//array.appendContentsOf([11,12,13])
//swift3.0
array.append(contentsOf:[11,12,13])
//Insert an element somewhere in the array
//swift2.2
//array.insert(0, atIndex: 0)
//swift3.0
array.insert(0, at: 0)
//Insert a set of elements into an array somewhere
//swift2.2
//array.insertContentsOf([-2,-1], at: 0)
//swift3.0
array.insert(contentsOf:[-2,-1], at: 0)
//Remove elements from an array at a certain location
//swift2.2
//array.removeAtIndex(1)
//swift3.0
array.remove(at: 1)
//Remove elements within a range
//swift2.2
//array.removeRange(0...2)
//swift3.0
array.removeSubrange(0...2)
//Modify elements within a scope
//swift2.2
//array.replaceRange(0...2, with:[0,1])
//swift3.0
array.replaceSubrange(0...2, with:[0,1])
//An array enumeration traversal will output (0,0) (1,1) (2,2) (3,3) (4,4)
//In swift 3.0, the enumerated attribute enumerate is changed to the enumerated() method
for item in arrayLet.enumerated(){
 print(item)
}
var arraySort = [1,3,5,6,7]
//Get the maximum in the array
//swift2.2
//arraySort.maxElement()
//swift3.0
arraySort.max()
//Get the minimum in an array
//swift2.2
//arraySort.minElement()
//swift3.0
arraySort.min()
//Ranking from large to small
//swift2.2
//arraySort = arraySort.sort(>)
//swift3.0
arraySort = arraySort.sorted(isOrderedBefore: >)
//Ranking from small to large
//swift2.2
//arraySort = arraySort.sort(<)
//swift3.0
arraySort = arraySort.sorted(isOrderedBefore: <)


Change in Set Collection

Examples of modifications in the Set collection are as follows:

//Create set collection
var set1:Set<Int> = [1,2,3,4]
//Move Subscripts
//Get an element after a subscript
//swlft2.2
//set1[set1.startIndex.successor()]
//swift3.0
set1[set1.index(after: set1.startIndex)]
//Get the elements after a subscript
//swift2.2
//set1[set1.startIndex.advancedBy(3)]
//swift3.0
set1[set1.index(set1.startIndex, offsetBy: 3)]
//Get the maximum value in the collection
//swift2.2
//set1.maxElement()
//swift3.0
set1.max()
//Get the minimum in the set
//swift2.2
//set1.minElement()
//swift3.0
set1.min()
//Remove elements from a location in a collection
//swift2.2
//set1.removeAtIndex(set1.indexOf(3)!)
//swift3.0
set1.remove(at: set1.index(of: 3)!)
var set3:Set<Int> = [1,2,3,4]
var set4:Set<Int> = [1,2,5,6]
//Return intersection {1,2}
//swift2.2
//var setInter = set3.intersect(set4)
//swift3.0
var setInter = set3.intersection(set4)
//Returns the intersection complement {3,4,5,6}
//swift2.2
//var setEx = set3.exclusiveOr(set4)
//swift3.0
var setEx = set3.symmetricDifference(set4)

var set5:Set = [1,2]
var set6:Set = [2,3]
var set7:Set = [1,2,3]
var set8:Set = [1,2,3]
//Determine whether a subset of a set, set5, is a subset of set7 that returns ture?
//swift2.2
//set5.isSubsetOf(set7)
//swift3.0
set5.isSubset(of: set7)
//Determine whether a superset set set 7 of a set is a superset return ture of set 5?
//swift2.2
//set7.isSupersetOf(set5)
//swift3.0
set7.isSuperset(of: set5)
//Determine whether the true subset of a set set5 is the true subset of set7 returns ture?
//swift2.2
//set5.isStrictSubsetOf(set7)
//swift3.0
set5.isStrictSubset(of: set7)
//Determine whether the true superset set set 7 of a set is not the true superset set set 8 returns false?
//swift2.2
//set7.isStrictSupersetOf(set8)
//swift3.0
set7.isStrictSuperset(of: set8)



IV. Changes in Basic Operators

The basic operators in Swift 3.0 have not changed much, but the floating-point redundancy function of the redundancy operator has been removed. It is a unique feature of Swift that the floating-point operation book of the redundancy operator can be carried out. With the change of version 3.0, the "%" operator function in Swift will be consistent with the redundancy operator in objective-c and C language.


Topics: Swift Programming Attribute C