初探Swift3.0帶來的變化匯總。本站提示廣大學習愛好者:(初探Swift3.0帶來的變化匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是初探Swift3.0帶來的變化匯總正文
廢話不多說了,直接給大家貼代碼了。
var string = "Hello-Swift" //獲取某個下標後一個下標對應的字符 char="e" //swift2.2 //var char = string[startIndex.successor()] //swift3.0 var char = string[string.index(after: startIndex)] //獲取某個下標前一個下標對應的字符 char2 = "t" //swift2.2 //var char2 = string[endIndex.predecessor()] //swift3.0 var char2 = string[string.index(before: string.endIndex)] //經過范圍獲取字符串中的一個子串 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] //獲取某個子串在父串中的范圍 //swift2.2 //var range = string.rangeOfString("Hello") //swift3.0 var range = string.range(of: "Hello") //追加字符串操作 此時string = "Hello-Swift! Hello-World" //swift2.2 //string.appendContentsOf(" Hello-World") //swift3.0 string.append(" Hello-World") //在指定地位拔出一個字符 此時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)) //在指定地位拔出一組字符 此時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)) //在指定范圍交換一個字符串 此時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" //swift2.2 //string.removeAtIndex(string.endIndex.predecessor()) //swift3.0 string.remove(at: string.index(before:string.endIndex)) //刪除指定范圍的字符 此時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" //全部轉換為大寫 //swift2.2 //string2 = string2.uppercaseString //swift3.0 string2 = string2.uppercased() //全部轉換為小寫 //swift2.2 //string2 = string2.lowercaseString //swift3.0 string2 = string2.lowercased()
需求留意,在Swift3.0中Range構造體被劃分紅了兩品種型,Range和ClosedRange,辨別用來描繪左閉右開區間和閉區間,對應到運算符為0..<10和0...10。
從下面的示例代碼中可以看出,String類型中的很多辦法命名停止了Swift作風的簡化,改動較大的一個點是關於下標index的改動,移除了兩個Index下標挪動的辦法,運用String類型的index()辦法來停止下標的挪動操作,編程愈加平安。
以上所述是給大家引見的初探Swift3.0帶來的變化匯總,希望對大家有所協助,假如大家有任何疑問請給我留言,會及時回復大家的。在此也十分感激大家對網站的支持!