]> Zhao Yanbai Git Server - acecode.git/commitdiff
convert swift from 2.2 to 3.0
authorAce <ace@Aces-MacBook-Pro.local>
Sun, 6 Nov 2016 01:38:17 +0000 (09:38 +0800)
committerAce <ace@Aces-MacBook-Pro.local>
Sun, 6 Nov 2016 01:38:17 +0000 (09:38 +0800)
13 files changed:
learn/AcePlay/AcePlay.playground/Pages/Basics.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Closure.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Sources/Utils.swift
learn/AcePlay/AcePlay.playground/Utils.remap [new file with mode: 0644]
learn/AcePlay/AcePlay.playground/contents.xcplayground
learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate
learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj
learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate
learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist [new file with mode: 0644]
learn/AppleSwift/AppleSwift/main.swift
learn/doc/mac_bash_profile

index 1ff1148e8dcfe7b83a86b03fb3ad17ff51e92877..1f82280371981991e60cdbe212cf7b8f6af9e3e4 100644 (file)
@@ -6,7 +6,7 @@ let str = "Hello, playground.小狗:🐶 锤子:🔨"
 
 // Index
 let strInx:String.Index = str.startIndex
-strInx.successor()
+str.index(after: strInx)
 print(strInx)
 
 for c in str.characters {
@@ -19,12 +19,12 @@ var company:Array<String> = [ "Apple", "Google", "Facebook", "Tencent" ]
 print(company[0], company[1], company[2], company[3], separator: "#", terminator: " $$$$$\n")
 
 printLine("Count company Array 1")
-for (i,v) in company.enumerate() {  // enumerate 返回的是 index value 组成的元组
+for (i,v) in company.enumerated() {  // enumerate 返回的是 index value 组成的元组
     print(i, v, separator: " - ", terminator: "\n")
 }
 
 printLine("Count company Array 2")
-company.insert("Alibaba", atIndex: company.count)
+company.insert("Alibaba", at: company.count)
 for i in 0..<company.count {
     print(i, company[i], separator: " - ")
 }
@@ -32,10 +32,10 @@ company.removeLast()
 
 var someIntsA: [Int] = []
 var someIntsB = [Int]()
-var someIntsC = [Int](count: 10, repeatedValue: 1)
+var someIntsC = [Int](repeating: 1, count: 10)
 someIntsA.append(1)
-someIntsB.replaceRange(Range<Int>(0..<someIntsB.count), with: [1,3,4])
-someIntsC.removeAtIndex(4)
+someIntsB.replaceSubrange(CountableRange<Int>(0..<someIntsB.count), with: [1,3,4])
+someIntsC.remove(at: 4)
 someIntsC[1...4] = [1, 2, 3, 4, 5, 6]  //实际赋值量可以与下标Range量不等
 
 printLine("Set")
@@ -68,7 +68,7 @@ if SetC.contains("Swift") {
     print("SetC Contains Swift")
 }
 
-for v in SetC.sort() {
+for v in SetC.sorted() {
     print(v)
 }
 
@@ -92,11 +92,11 @@ for (k, v) in DictD {
     print("Key:", k, " Value: ", v)
 }
 
-for key in DictD.keys.sort() {
+for key in DictD.keys.sorted() {
     print(key)
 }
 
-for value in DictD.values.sort() {
+for value in DictD.values.sorted() {
     print(value)
 }
 
index cd5b70d047ff751e1631095e942df297550e58e3..6957e7eaf38db065aa30a05e9ac3b15277b75050 100644 (file)
@@ -28,38 +28,38 @@ var company = ["Tencent", "Apple", "Facebook", "Google", "Twitter", "Amazon"]
 var sortedCompany: [String] = []
 
 printLine("Sort")
-sortedCompany = company.sort()
+sortedCompany = company.sorted()
 print(sortedCompany)
 sortedCompany = []
 
 printLine("Sort With Function A")
-func backwardsA(a: String, b: String) -> Bool {
+func backwardsA(a: String, b: String) -> Bool {
     return a > b
 }
-sortedCompany = company.sort(backwardsA)
+sortedCompany = company.sorted(by: backwardsA)
 print(sortedCompany)
 sortedCompany = []
 
 printLine("Sort With Backwards Closure A [Closure Expression Syntax]")
-sortedCompany = company.sort({ (a: String, b: String) -> Bool in return a>b })
+sortedCompany = company.sorted(by: { (a: String, b: String) -> Bool in return a>b })
 print(sortedCompany)
 sortedCompany = []
 
 // 参数及返回类型自动推断
 printLine("Sort With Backwards Closure B [Inferring Type From Context]")
-sortedCompany = company.sort({ a, b in return a > b })
+sortedCompany = company.sorted(by: { a, b in return a > b })
 print(sortedCompany)
 sortedCompany = []
 
 // 隐式返回表达式闭包,省略return
 printLine("Sort With Backwards Closure C [Implicit Returns from Single-Expression Closures]")
-sortedCompany = company.sort({ a, b in a > b })
+sortedCompany = company.sorted(by: { a, b in a > b })
 print(sortedCompany)
 sortedCompany = []
 
 // 简写参数名
 printLine("Sort With Backwards Closure D [Shorthand Argument Names]")
-sortedCompany = company.sort({ $0 > $1 })
+sortedCompany = company.sorted(by: { $0 > $1 })
 print(sortedCompany)
 sortedCompany = []
 
@@ -67,13 +67,13 @@ sortedCompany = []
  There’s actually an even shorter way to write the closure expression above. Swift’s String type defines its string-specific implementation of the greater-than operator (>) as a function that has two parameters of type String, and returns a value of type Bool. This exactly matches the function type needed by the sort(_:) method. Therefore, you can simply pass in the greater-than operator, and Swift will infer that you want to use its string-specific implementation:
  */
 printLine("Sort With Backwards Closure E [Operator Functions]")
-sortedCompany = company.sort(>)
+sortedCompany = company.sorted(by: >)
 print(sortedCompany)
 sortedCompany = []
 
 // Trailing Closure
 printLine("Sort With Backwards Closure F [Trailing Closure]")
-sortedCompany = company.sort() { a, b in a > b} // 如果闭包参数是这个函数的最后一个参数,是可以采用尾随闭包写法
+sortedCompany = company.sorted() { a, b in a > b} // 如果闭包参数是这个函数的最后一个参数,是可以采用尾随闭包写法
 //sortedCompany = company.sort { a, b in a > b} // 如果闭包参数是这个函数的唯一一个参数,是可以不用写括号的
 print(sortedCompany)
 sortedCompany = []
@@ -110,7 +110,7 @@ print(digitString)
 printLine("Captuare Value")
 
 // 捕获值
-func makeIncrementer(step:Int) -> () -> Int {
+func makeIncrementer(step:Int) -> () -> Int {
     var total = 0
     func inc() -> Int {
         total += step
@@ -132,12 +132,12 @@ print("ClosureFuncC:", closureFuncC())
 
 // 逃逸&非逃逸闭包
 printLine("Noescaping & Escaping Closesure")
-func noescapingClosure(@noescape closure: () -> Void) {
+func noescapingClosure(_ closure: () -> Void) {
     closure()
 }
 
 var closureHandler: Array<() -> Void> = []
-func escapingClosure(closure: () -> Void) {  // 此时参数前加@noescape会报错
+func escapingClosure(_ closure: @escaping () -> Void) {  // 此时参数前加@noescape会报错
     closureHandler.append(closure)
 }
 
@@ -161,7 +161,7 @@ printLine("AutoClosure")
 print("Now Company Items:", company)
 print("Company Item Count:", company.count)
 // autoClosureHanlerA的type是 () -> String 不是 String
-let autoClosureHandlerA = { company.removeAtIndex(0) }  // an autoclosure lets you delay evaluation
+let autoClosureHandlerA = { company.remove(at: 0) }  // an autoclosure lets you delay evaluation
 print("Company Item Count:", company.count)
 print("No Remove \(autoClosureHandlerA())")
 print("Company Item Count:", company.count)
@@ -169,30 +169,30 @@ print("Company Item Count:", company.count)
 
 // autoclosure parameter
 printLine("AutoClosure Parameter")
-func autoClosureFuncParameterA(closure: () -> String) {
+func autoClosureFuncParameterA(closure: () -> String) {
     print("AutoClosureFuncParameterA \(closure())!")
 }
-autoClosureFuncParameterA({ company.removeAtIndex(0) })
+autoClosureFuncParameterA({ company.remove(at: 0) })
 
-func autoClosureFuncParameterB(@autoclosure closure: () -> String) {
+func autoClosureFuncParameterB(_ closure: @autoclosure () -> String) {
     print("AutoClosureFuncParameterB \(closure())!")
 }
-autoClosureFuncParameterB(company.removeAtIndex(0))
+autoClosureFuncParameterB(company.remove(at: 0))
 
 // @autoclosure 暗含了 noescape 特性
 var autoClosureHanlder: [() -> String] = []
-func autoClosureFuncParameterC(@autoclosure closure: () -> String) {
+func autoClosureFuncParameterC(_ closure: @autoclosure () -> String) {
     //因为参数被@autoclosure修饰了,而@autoclosure暗含@noescape特性,因此以下语句会报错
     //autoClosureHanlder.append(closure)
 }
 
 // 如果用了@autoclosure又要用escape特性,则用@autoclosure(escaping)修饰参数
-func autoClosureFuncParameterD(@autoclosure(escaping) closure: () ->String) {
+func autoClosureFuncParameterD( _ closure: @autoclosure @escaping () ->String) {
     print("Called autoClosureFuncParameterD")
     autoClosureHanlder.append(closure)
 }
-autoClosureFuncParameterD(company.removeAtIndex(0))
-autoClosureFuncParameterD(company.removeAtIndex(0))
+autoClosureFuncParameterD(company.remove(at: 0))
+autoClosureFuncParameterD(company.remove(at: 0))
 
 for handler in autoClosureHanlder {
     print("autoClosure Handling \(handler())!")
index 34b3a91f2a5e129c0cba6ba48927eb1e26b9a261..3ce322cd854003c70de6be5984b583f9fe4382a8 100644 (file)
@@ -3,37 +3,37 @@
 import UIKit
 
 enum CompassPoint {
-    case North
-    case East
-    case South
-    case West
+    case north
+    case east
+    case south
+    case west
 }
 
-var directionToHead = CompassPoint.North
-directionToHead = .East
+var directionToHead = CompassPoint.north
+directionToHead = .east
 
 switch directionToHead {
-    case .North: print("Losts of Plantes Have a North")
-    case .East : print("Where the Sun Rises")
-    case .South: print("Watch Out for Penguins")
-    case .West : print("Where the Skies are Blue")
+    case .north: print("Losts of Plantes Have a North")
+    case .east : print("Where the Sun Rises")
+    case .south: print("Watch Out for Penguins")
+    case .west : print("Where the Skies are Blue")
 }
 
 
 // Associated Values
 printLine("Enumeration Associated Values")
 enum BarCode {
-    case UPCA(Int, Int, Int, Int)
-    case QRCode(String)
+    case upca(Int, Int, Int, Int)
+    case qrCode(String)
 }
 
-var productBarCode = BarCode.UPCA(8, 88488, 66366, 2)
-productBarCode = .QRCode("QRCode-123456")
+var productBarCode = BarCode.upca(8, 88488, 66366, 2)
+productBarCode = .qrCode("QRCode-123456")
 
 switch productBarCode {
-case let .UPCA(numberSystem, manufacturer, product, check) :
+case let .upca(numberSystem, manufacturer, product, check) :
     print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check)")
-case let .QRCode(productCode) :
+case let .qrCode(productCode) :
     print("QR Code: \(productBarCode)")
 }
 
@@ -41,26 +41,26 @@ case let .QRCode(productCode) :
 printLine("RawValues")
 // Raw Values
 enum ASCIIControlCharacter: Character {
-    case Tab            = "\t"
-    case LineFeed       = "\n"
-    case CarriageReturn = "\r"
+    case tab            = "\t"
+    case lineFeed       = "\n"
+    case carriageReturn = "\r"
 }
 
 // Implicitly Assigned Raw Values
 // 整型从0开始自增1,每个项的rawValue不能相同
 enum Planet: Int {
-    case Mercury
-    case Venus
-    case Earth
-    case Mars
-    case Jupiter
-    case Saturn
-    case Uranus
-    case Netpune
+    case mercury
+    case venus
+    case earth
+    case mars
+    case jupiter
+    case saturn
+    case uranus
+    case netpune
 }
 
-print(Planet.Mercury, Planet.Mercury.rawValue)
-print(Planet.Mars, Planet.Mars.rawValue)
+print(Planet.mercury, Planet.mercury.rawValue)
+print(Planet.mars, Planet.mars.rawValue)
 print(Planet(rawValue: 5)!, Planet(rawValue: 5)!.rawValue)
 
 
@@ -82,21 +82,21 @@ print(CompassPointString.West,  CompassPointString.West.rawValue)
 printLine("Resursive Enumerations")
 // 不能忘记 indirect
 indirect enum Exp {
-    case Number(Int)
-    case Add(Exp, Exp)  // indirect 也可以放在case语句之前
-    case Mul(Exp, Exp)
+    case number(Int)
+    case add(Exp, Exp)  // indirect 也可以放在case语句之前
+    case mul(Exp, Exp)
 }
 
 
-let exp = Exp.Mul(Exp.Add(Exp.Number(2), Exp.Number(5)), Exp.Number(7))
+let exp = Exp.mul(Exp.add(Exp.number(2), Exp.number(5)), Exp.number(7))
 
-func evaluateExp(exp: Exp) -> Int {
+func evaluateExp(exp: Exp) -> Int {
     switch exp {
-    case let .Number(n):
+    case let .number(n):
         return n
-    case let .Add(a, b) :
+    case let .add(a, b) :
         return evaluateExp(a) + evaluateExp(b)
-    case let .Mul(a, b):
+    case let .mul(a, b):
         return evaluateExp(a) * evaluateExp(b)
     }
 }
index ebcfc6ef9e370548a2cd930a89de273a4b487bbf..d248acc304f233865796b76d400ce8e52ad0ecf9 100644 (file)
@@ -14,7 +14,7 @@ Hello()
 
 printLine("Multiple Return Types")
 
-func minMax(data:Array<Int>) -> (min:Int, max:Int)? {
+func minMax(data:Array<Int>) -> (min:Int, max:Int)? {
     guard data.count > 0 else {
         return nil
     }
@@ -43,7 +43,7 @@ func sameExternalParameterNames( ExName a: Int, ExName b: Int) -> Int {
 print(sameExternalParameterNames(ExName: 10, ExName: 20))
 
 // 引用传参
-func swapTwoInts(inout a: Int, inout _ b: Int) -> Void {
+func swapTwoInts(_ a: inout Int, _ b: inout Int) -> Void {
     let t: Int = a
     a = b
     b = t
index ece3a6febcefe1a887076d26af87788a1f864598..8c6499ae882e8eb97e2bc1d2eb7c3d4afcb29323 100644 (file)
@@ -1,7 +1,7 @@
 import UIKit
 
 public
-func printLine(title: String) -> Void {
+func printLine(title: String) -> Void {
     let line = String(format:"[%@]-----------------------------------", title)
     print(line)
-}
\ No newline at end of file
+}
diff --git a/learn/AcePlay/AcePlay.playground/Utils.remap b/learn/AcePlay/AcePlay.playground/Utils.remap
new file mode 100644 (file)
index 0000000..2b2694d
--- /dev/null
@@ -0,0 +1,7 @@
+[
+ {
+  "file": "/Users/ace/workspace/acecode/learn/AcePlay/AcePlay.playground/Sources/Utils.swift",
+  "offset": 36,
+  "text": "_ ",
+ }
+]
index d6d3e1a699042a2b652be7fa913780bfe147381c..91406914fc81d54709e36eeb5edb44cfc167b28d 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<playground version='6.0' target-platform='ios'>
+<playground version='6.0' target-platform='ios' last-migration='0810'>
     <pages>
         <page name='Functions'/>
         <page name='Basics'/>
index ba649d1f6a0ca166f8e94b340263043004708176..83c223dd43a77d0ca64359149882678d75458905 100644 (file)
Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ
index c288f4c3c30c3cffa276dc63015030d102d46d6e..fced780d7771fafd1192a43da60d976f68d9a611 100644 (file)
@@ -95,6 +95,7 @@
                                TargetAttributes = {
                                        5063B2A11B390AAF009C5821 = {
                                                CreatedOnToolsVersion = 6.3.2;
+                                               LastSwiftMigration = 0810;
                                        };
                                };
                        };
                        isa = XCBuildConfiguration;
                        buildSettings = {
                                PRODUCT_NAME = "$(TARGET_NAME)";
+                               SWIFT_VERSION = 3.0;
                        };
                        name = Debug;
                };
                        isa = XCBuildConfiguration;
                        buildSettings = {
                                PRODUCT_NAME = "$(TARGET_NAME)";
+                               SWIFT_VERSION = 3.0;
                        };
                        name = Release;
                };
index d2eabf626f060670834e1ba0c3c2233a90568b56..bb26f9734b72b24ab42034dd4560642fa9ed7102 100644 (file)
Binary files a/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
new file mode 100644 (file)
index 0000000..7a7ffc6
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Bucket
+   type = "1"
+   version = "2.0">
+   <Breakpoints>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            shouldBeEnabled = "No"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "AppleSwift/main.swift"
+            timestampString = "500087121.884596"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "40"
+            endingLineNumber = "40">
+         </BreakpointContent>
+      </BreakpointProxy>
+   </Breakpoints>
+</Bucket>
index 15506f860cb391d5da18d1b6a2ce86a56c7b3731..d02413e460964cf8b8ea7e3c8d7411c6ffbd7ae4 100644 (file)
@@ -37,15 +37,15 @@ var dict = [
     "tencnet" : "CN"
 ]
 print(dict)
-print(dict["apple"])
+print(dict["google"] ?? "default value")
 print(dict["apple"]!)
 
 // 创建空数组和空字典
 var EmptyArray = [String]()
 var EmptyDict  = [String:String]()
 // or
-var EmptArrayWithNoType = []
-var EmptDictWithNoType = [:]
+//var EmptArrayWithNoType = []
+//var EmptDictWithNoType = [:]
 
 for cp in arrayList {
     print(cp)
@@ -141,8 +141,8 @@ print("  emojistr \(emojiStr) charactor count is \(emojiStr.characters.count)")
 
 s = "Prefix----Suffix"
 if s.hasPrefix("Prefix") && s.hasSuffix("Suffix") {
-    print(s.lowercaseString)
-    print(s.uppercaseString)
+    print(s.lowercased())
+    print(s.uppercased())
 }
 
 
@@ -154,11 +154,11 @@ for i in 1...5 {
 IntArray += [0, -1, -2, -3, -4]
 IntArray[0] = 1024
 IntArray[1..<4]=[999, 888, 777]
-IntArray.insert(1, atIndex: 0)
-IntArray.removeAtIndex(5)
+IntArray.insert(1, at: 0)
+IntArray.remove(at: 5)
 IntArray.removeLast()
 print(IntArray)
-for (index, value) in IntArray.enumerate() {
+for (index, value) in IntArray.enumerated() {
     print("Item \(index+1): \(value)")
 }
 
@@ -167,7 +167,7 @@ var StringArray = [String]()
 StringArray.append("dd")
 StringArray = []
 
-var DoubleArray = [Double](count:3, repeatedValue:9.9)
+var DoubleArray = [Double](repeating: 9.9, count: 3)
 for i in 1...5 {
     DoubleArray.append(Double(i)*1.1)
 }
@@ -186,14 +186,14 @@ if SetA.contains(1) {
     print(SetA)
 }
 
-for genre in SetA.sort() {
+for genre in SetA.sorted() {
     print("\(genre)")
 }
 
 let oddSet:Set = [1, 3, 5, 7, 9]
 let evenSet:Set = [0, 2, 4, 6, 8]
-print(oddSet.intersect(evenSet).sort())  // 交
-print(oddSet.union(evenSet).sort())      // 并
+print(oddSet.intersection(evenSet).sorted())  // 交
+print(oddSet.union(evenSet).sorted())      // 并
 print(oddSet.hashValue)
 
 
@@ -221,7 +221,7 @@ print(airports)
 airports["APL"] = nil   // Delete
 print(airports)
 
-if let removedValue = airports.removeValueForKey("DUB") {
+if let removedValue = airports.removeValue(forKey: "DUB") {
     print("The removed airport's name is \(removedValue)")
 } else {
     print("The airports dictionary does not contain a value for DUB")
index 63c5e2121e295a11c905ddbd7062a8f733ff24bc..591b4fb883a0f911ec7aa0e3b6b4db0bdbbb3fe1 100644 (file)
@@ -3,7 +3,7 @@
 HISTCONTROL=ignoreboth
 
 export PATH=/usr/local/sbin:$PATH
-export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"
+#export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"
 export PATH=/Users/Ace/.local/bin:$PATH
 export GOBIN=/Users/Ace/.local/bin
 export PATH=/Users/Ace/workspace/github/metasploit-framework:$PATH
@@ -14,6 +14,7 @@ export PS1="\W\$ "
 alias ll='ls -lGh'
 alias ls='ls -Gh'
 alias rm='rm -rf'
+alias cls='clear'
 alias grep='grep --color'
 alias egrep='egrep --color'
 alias msf='msfconsole'