From: Ace Date: Sun, 6 Nov 2016 01:38:17 +0000 (+0800) Subject: convert swift from 2.2 to 3.0 X-Git-Url: http://zhaoyanbai.com/repos/COPYRIGHT?a=commitdiff_plain;h=692255659f3d737fad64f92df5595f975ef1c694;p=acecode.git convert swift from 2.2 to 3.0 --- diff --git a/learn/AcePlay/AcePlay.playground/Pages/Basics.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Basics.xcplaygroundpage/Contents.swift index 1ff1148..1f82280 100644 --- a/learn/AcePlay/AcePlay.playground/Pages/Basics.xcplaygroundpage/Contents.swift +++ b/learn/AcePlay/AcePlay.playground/Pages/Basics.xcplaygroundpage/Contents.swift @@ -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 = [ "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..(0..(0.. 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())!") diff --git a/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift index 34b3a91..3ce322c 100644 --- a/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift +++ b/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift @@ -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) } } diff --git a/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift index ebcfc6e..d248acc 100644 --- a/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift +++ b/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift @@ -14,7 +14,7 @@ Hello() printLine("Multiple Return Types") -func minMax(data:Array) -> (min:Int, max:Int)? { +func minMax(_ data:Array) -> (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 diff --git a/learn/AcePlay/AcePlay.playground/Sources/Utils.swift b/learn/AcePlay/AcePlay.playground/Sources/Utils.swift index ece3a6f..8c6499a 100644 --- a/learn/AcePlay/AcePlay.playground/Sources/Utils.swift +++ b/learn/AcePlay/AcePlay.playground/Sources/Utils.swift @@ -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 index 0000000..2b2694d --- /dev/null +++ b/learn/AcePlay/AcePlay.playground/Utils.remap @@ -0,0 +1,7 @@ +[ + { + "file": "/Users/ace/workspace/acecode/learn/AcePlay/AcePlay.playground/Sources/Utils.swift", + "offset": 36, + "text": "_ ", + } +] diff --git a/learn/AcePlay/AcePlay.playground/contents.xcplayground b/learn/AcePlay/AcePlay.playground/contents.xcplayground index d6d3e1a..9140691 100644 --- a/learn/AcePlay/AcePlay.playground/contents.xcplayground +++ b/learn/AcePlay/AcePlay.playground/contents.xcplayground @@ -1,5 +1,5 @@ - + diff --git a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate index ba649d1..83c223d 100644 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 diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj b/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj index c288f4c..fced780 100644 --- a/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj +++ b/learn/AppleSwift/AppleSwift.xcodeproj/project.pbxproj @@ -95,6 +95,7 @@ TargetAttributes = { 5063B2A11B390AAF009C5821 = { CreatedOnToolsVersion = 6.3.2; + LastSwiftMigration = 0810; }; }; }; @@ -209,6 +210,7 @@ isa = XCBuildConfiguration; buildSettings = { PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -216,6 +218,7 @@ isa = XCBuildConfiguration; buildSettings = { PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AppleSwift/AppleSwift.xcodeproj/project.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate index d2eabf6..bb26f97 100644 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 index 0000000..7a7ffc6 --- /dev/null +++ b/learn/AppleSwift/AppleSwift.xcodeproj/xcuserdata/Ace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/learn/AppleSwift/AppleSwift/main.swift b/learn/AppleSwift/AppleSwift/main.swift index 15506f8..d02413e 100644 --- a/learn/AppleSwift/AppleSwift/main.swift +++ b/learn/AppleSwift/AppleSwift/main.swift @@ -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") diff --git a/learn/doc/mac_bash_profile b/learn/doc/mac_bash_profile index 63c5e21..591b4fb 100644 --- a/learn/doc/mac_bash_profile +++ b/learn/doc/mac_bash_profile @@ -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'