// Index
let strInx:String.Index = str.startIndex
-strInx.successor()
+str.index(after: strInx)
print(strInx)
for c in str.characters {
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: " - ")
}
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")
print("SetC Contains Swift")
}
-for v in SetC.sort() {
+for v in SetC.sorted() {
print(v)
}
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)
}
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 = []
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 = []
printLine("Captuare Value")
// 捕获值
-func makeIncrementer(step:Int) -> () -> Int {
+func makeIncrementer(_ step:Int) -> () -> Int {
var total = 0
func inc() -> Int {
total += step
// 逃逸&非逃逸闭包
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)
}
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)
// 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())!")
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)")
}
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)
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)
}
}
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
}
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
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
+}
--- /dev/null
+[
+ {
+ "file": "/Users/ace/workspace/acecode/learn/AcePlay/AcePlay.playground/Sources/Utils.swift",
+ "offset": 36,
+ "text": "_ ",
+ }
+]
<?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'/>
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;
};
--- /dev/null
+<?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>
"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)
s = "Prefix----Suffix"
if s.hasPrefix("Prefix") && s.hasSuffix("Suffix") {
- print(s.lowercaseString)
- print(s.uppercaseString)
+ print(s.lowercased())
+ print(s.uppercased())
}
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)")
}
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)
}
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)
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")
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
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'