Difference between revisions of "Swift Introduction"
From Hawk Wiki
(→Constant) |
(→Constant and Variables) |
||
(18 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Swift Basic= | =Swift Basic= | ||
+ | Back To [[IOS_Swift]] | ||
==Var== | ==Var== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
var languageName = "Swift" // is simplified version of var languageName: String = "Swift" | var languageName = "Swift" // is simplified version of var languageName: String = "Swift" | ||
</pre> | </pre> | ||
==Constant== | ==Constant== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
let version = 1.0 // let version: Double = 1.0 | let version = 1.0 // let version: Double = 1.0 | ||
//:Int, :Bool. :String | //:Int, :Bool. :String | ||
Line 15: | Line 16: | ||
==Examples== | ==Examples== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
let a = 3, b = 5 | let a = 3, b = 5 | ||
let res = "\(a) times \(b) is \(a * b)" | let res = "\(a) times \(b) is \(a * b)" | ||
Line 24: | Line 25: | ||
let ab = a + b | let ab = a + b | ||
</pre> | </pre> | ||
− | ==Array== | + | ==Array & Dictionary== |
− | <pre class="brush: | + | <pre class="brush:swift"> |
+ | //Empty array | ||
+ | var activeInputs: [CIImage] = [] //Must explicitly specify type | ||
+ | //mixed type | ||
var names = ["Anna", "Alex", 42] | var names = ["Anna", "Alex", 42] | ||
var names: String[] = ["Anna", "Alex"] | var names: String[] = ["Anna", "Alex"] | ||
+ | //Dictionary | ||
var numOfLegs = ["ant": 6, "snake": 0] | var numOfLegs = ["ant": 6, "snake": 0] | ||
+ | //Interate | ||
for x in names {} | for x in names {} | ||
for number in 1...5 {} | for number in 1...5 {} | ||
Line 40: | Line 46: | ||
numOfLegs["spider"] = 273 | numOfLegs["spider"] = 273 | ||
numOfLegs["spider"] = 8 | numOfLegs["spider"] = 8 | ||
+ | //Add to array | ||
+ | names.append("ABC") | ||
</pre> | </pre> | ||
+ | ===Initialize Empty Array or Initialize With Content=== | ||
+ | <pre class="brush:swift"> | ||
+ | // Initialize empty array | ||
+ | var array = [String]() | ||
+ | // Initialize empty Dictionary | ||
+ | var dict = Dictionary<String, String>() | ||
+ | // Initialize Array with content and length | ||
+ | var threeDoubles = [Double](count: 3, repeatedValue: 0.0) | ||
+ | </pre> | ||
+ | |||
==Optionals== | ==Optionals== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
let possibleLegs: Int? = num | let possibleLegs: Int? = num | ||
OfLegs["aardvark"] //either get integer or nothing | OfLegs["aardvark"] //either get integer or nothing | ||
Line 64: | Line 82: | ||
func sayHello(name:String = "default") { //give a default value | func sayHello(name:String = "default") { //give a default value | ||
println("Hello \(name)") | println("Hello \(name)") | ||
+ | } | ||
+ | </pre> | ||
+ | ===Optionals in Swift 1.2=== | ||
+ | <pre class="brush:swift"> | ||
+ | // Before Swift 1.2 | ||
+ | if let data = widget.dataStream { | ||
+ | if data.isValid { | ||
+ | if let source = sourceIP() { | ||
+ | if let dest = destIP() { | ||
+ | // do something | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | <pre class="brush:swift"> | ||
+ | // After Swift 1.2 | ||
+ | if let data = widget.data where data.isValid, let source = sourceIP(), dest = destIP() { | ||
+ | // do something | ||
} | } | ||
</pre> | </pre> | ||
==Tuples== | ==Tuples== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
(404, "Not found") | (404, "Not found") | ||
(2, "Banana", 0.42) | (2, "Banana", 0.42) | ||
let (statusCode, message) = refresh() | let (statusCode, message) = refresh() | ||
</pre> | </pre> | ||
+ | |||
==Closures== | ==Closures== | ||
− | <pre class="brush: | + | Closure has implicit return! <br> |
− | func repeat(count: Int, task: () -> ()) { | + | If you get an error from xcode related to wrong return type, try return something explicitly. |
− | + | <pre class="brush:swift"> | |
− | + | func repeat(count: Int, task: (idx: Int) -> ()) { | |
− | + | for i in 0..<count { | |
+ | task(idx: i) | ||
+ | } | ||
} | } | ||
− | repeat(2, { | + | repeat(2, { idx in |
− | + | println("hello: " + String(idx)) | |
}) | }) | ||
− | |||
− | |||
− | |||
</pre> | </pre> | ||
+ | |||
==Class== | ==Class== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
class Vehicle { | class Vehicle { | ||
var numOfWheels = 0 //stored properties | var numOfWheels = 0 //stored properties | ||
Line 104: | Line 142: | ||
numOfWheels = 2 | numOfWheels = 2 | ||
} | } | ||
+ | //properties observers | ||
override var speed: Double { | override var speed: Double { | ||
willSet { | willSet { | ||
Line 113: | Line 152: | ||
//oldValue available | //oldValue available | ||
} | } | ||
+ | } | ||
+ | //method | ||
+ | func accel() { | ||
+ | speed += 0.1 | ||
} | } | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | ==Overriding properties== | ||
+ | <pre class="brush:swift"> | ||
+ | class Car: Vehicle { | ||
+ | var speed = 0.0 | ||
+ | init() { | ||
+ | super.init() | ||
+ | numOfWheels = 4 | ||
+ | } | ||
+ | override var description: String { | ||
+ | return super.description + ", Speed=\(speed) mph" | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
==Struct== | ==Struct== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
+ | struct Point { | ||
+ | var x: Double, y: Double | ||
+ | } | ||
struct Rect { | struct Rect { | ||
var origin: Point | var origin: Point | ||
var size: Size | var size: Size | ||
+ | //computed property | ||
var area: Double { | var area: Double { | ||
return size.width * size.height | return size.width * size.height | ||
} | } | ||
+ | //method | ||
func isBiggerThanRect(other: Rect) => Bool { | func isBiggerThanRect(other: Rect) => Bool { | ||
return self.area > other.area | return self.area > other.area | ||
} | } | ||
} | } | ||
+ | //init | ||
+ | var point = Point(x: 0.0, y: 0.0) | ||
+ | var rect = Rect(origin: point, size: size) | ||
</pre> | </pre> | ||
Line 135: | Line 201: | ||
2. class is passed by reference. struct is passed by value | 2. class is passed by reference. struct is passed by value | ||
==Constant and Variables== | ==Constant and Variables== | ||
− | <pre class="brush: | + | <pre class="brush:swift"> |
let window = Window(1.0, 1.0) | let window = Window(1.0, 1.0) | ||
//The reference is a constant, cannot change the reference value | //The reference is a constant, cannot change the reference value | ||
Line 141: | Line 207: | ||
cannot do window = Window(2.0, 2.0) | cannot do window = Window(2.0, 2.0) | ||
− | var point1 = Point(x: 0.0, y: 0.0) //Point is struct. can do point1.x = 1.0 | + | var point1 = Point(x: 0.0, y: 0.0) // Point is struct. Since point1 is variable, we can do point1.x = 1.0 |
− | let point2 = Point(x: 0.0, y: 0.0) // | + | let point2 = Point(x: 0.0, y: 0.0) // Cannot do point2.x = 1.0 since point2 is constant |
+ | </pre> | ||
+ | ===Swift 1.2 conditional assign constant value=== | ||
+ | <pre class="brush:swift"> | ||
+ | let longEdge: CGFloat | ||
+ | |||
+ | if isLandscape { | ||
+ | longEdge = image.calculateWidth() | ||
+ | } else { | ||
+ | longEdge = image.calculateHeight() | ||
+ | } | ||
+ | |||
+ | // you can access longEdge from this point on | ||
+ | </pre> | ||
+ | ==Mutating a Structure== | ||
+ | <pre class="brush:swift"> | ||
+ | struct Point { | ||
+ | var x,y: Double | ||
+ | //tell the complier this is a mutating function | ||
+ | mutating func moveToRightBy(dx: Double) { | ||
+ | x += dx | ||
+ | } | ||
+ | } | ||
+ | let point1 = Point(x: 0.0, y: 0.0) | ||
+ | point.moveToRightBy(20.0) | ||
+ | //Error: Cannot mutate constant | ||
+ | </pre> | ||
+ | ==Enums== | ||
+ | <pre class="brush:swift"> | ||
+ | enum Plannet: Int { | ||
+ | case Mecury = 1, Venus | ||
+ | case Earth = 5 | ||
+ | } | ||
+ | let earth = Planet.Earth.toRaw(); | ||
+ | enum Direction { | ||
+ | case west, east, north, south | ||
+ | } | ||
+ | var direction = Direction.west | ||
+ | direction = .east | ||
+ | //example | ||
+ | label.testAlignment = .Right | ||
+ | |||
+ | </pre> | ||
+ | ==Enumerations: Associated Values== | ||
+ | <pre class="brush:swift"> | ||
+ | enum FlightStatus { | ||
+ | case OnTime | ||
+ | case Delayed(Int) // minutes | ||
+ | init() { | ||
+ | self = OnTime | ||
+ | } | ||
+ | var desc: String { | ||
+ | switch self { | ||
+ | case OnTime: | ||
+ | return "on time" | ||
+ | case Delayed(let minutes): | ||
+ | return "deplayed by \(minutes) minute(s)" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | var status = FlightStatus // equals to var status = FlightStatus.OnTime | ||
+ | status = .Delayed(30) | ||
+ | status.desc | ||
+ | //deplayed by 30 minute(s) | ||
+ | //Nested Type | ||
+ | class Flight { | ||
+ | enum Status { | ||
+ | case OnTime, Deplayed(Int) | ||
+ | ... | ||
+ | } | ||
+ | var status = Status.OnTime | ||
+ | } | ||
+ | </pre> | ||
+ | ==Extensions== | ||
+ | <pre class="brush:swift"> | ||
+ | extension Size { | ||
+ | mutating func scaleByFactor(factor: Int) { | ||
+ | width *= factor | ||
+ | height *= factor | ||
+ | } | ||
+ | } | ||
+ | extension Int { | ||
+ | func repetitions(task: () -> ()) { | ||
+ | for i in 0..<self { | ||
+ | task() | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | 400.repetitions { | ||
+ | println("Hello!") | ||
+ | } | ||
+ | </pre> | ||
+ | ==Generic Stack Structure (Templates)== | ||
+ | <pre class="brush:swift"> | ||
+ | struct Stack<T> { | ||
+ | var elements = <T>[]() | ||
+ | func push {...} | ||
+ | func pop {...} | ||
+ | } | ||
+ | var intStack = Stack<int>() | ||
+ | intStack.push(60) | ||
+ | let lastIn = intStack.pop() | ||
</pre> | </pre> |
Latest revision as of 17:07, 10 April 2015
Contents
- 1 Swift Basic
- 1.1 Var
- 1.2 Constant
- 1.3 Examples
- 1.4 Array & Dictionary
- 1.5 Optionals
- 1.6 Tuples
- 1.7 Closures
- 1.8 Class
- 1.9 Overriding properties
- 1.10 Struct
- 1.11 Struct and Class
- 1.12 Constant and Variables
- 1.13 Mutating a Structure
- 1.14 Enums
- 1.15 Enumerations: Associated Values
- 1.16 Extensions
- 1.17 Generic Stack Structure (Templates)
Swift Basic
Back To IOS_Swift
Var
var languageName = "Swift" // is simplified version of var languageName: String = "Swift"
Constant
let version = 1.0 // let version: Double = 1.0 //:Int, :Bool. :String let components = "~/Documents/Swift".pathComponents //["~", "Documents", "Swift"]
Examples
let a = 3, b = 5 let res = "\(a) times \(b) is \(a * b)" let dog = "dog" var dog1 = dog + "1" let a: Character = "a" let b: Character = "b" let ab = a + b
Array & Dictionary
//Empty array var activeInputs: [CIImage] = [] //Must explicitly specify type //mixed type var names = ["Anna", "Alex", 42] var names: String[] = ["Anna", "Alex"] //Dictionary var numOfLegs = ["ant": 6, "snake": 0] //Interate for x in names {} for number in 1...5 {} for num in 0..<5 {} //half close range for (name, legs) in numOfLegs {} //Tuple //Modify Array names += "Sam" names += ["Hao", "Dan"] names[0] = "x" names[3...5] = ["x", "y", "z"] numOfLegs["spider"] = 273 numOfLegs["spider"] = 8 //Add to array names.append("ABC")
Initialize Empty Array or Initialize With Content
// Initialize empty array var array = [String]() // Initialize empty Dictionary var dict = Dictionary<String, String>() // Initialize Array with content and length var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
Optionals
let possibleLegs: Int? = num OfLegs["aardvark"] //either get integer or nothing if possibleLegs == nil { } else { let legCount = possibleLegs! //forcing optional value to it's type } //A Simpler way if let legCount = possibleLegs { } Switch legCount { case 0: //no break case 1: case ExecButton: //match object case 1...5: case 2,4,6,8: default: //required } func sayHello(name:String = "default") { //give a default value println("Hello \(name)") }
Optionals in Swift 1.2
// Before Swift 1.2 if let data = widget.dataStream { if data.isValid { if let source = sourceIP() { if let dest = destIP() { // do something } } } }
// After Swift 1.2 if let data = widget.data where data.isValid, let source = sourceIP(), dest = destIP() { // do something }
Tuples
(404, "Not found") (2, "Banana", 0.42) let (statusCode, message) = refresh()
Closures
Closure has implicit return!
If you get an error from xcode related to wrong return type, try return something explicitly.
func repeat(count: Int, task: (idx: Int) -> ()) { for i in 0..<count { task(idx: i) } } repeat(2, { idx in println("hello: " + String(idx)) })
Class
class Vehicle { var numOfWheels = 0 //stored properties var description: String { //computed properties, has to be var get { //get is optional return "\(numOfWheels) wheels" } } } let someVehicle = Vehicle() //Auto alloc //inheritance class Bicycle: Vehicle { init() { //initializer super:init() numOfWheels = 2 } //properties observers override var speed: Double { willSet { if newValue > 65.0 { println('warning too fast') } } didSet { //oldValue available } } //method func accel() { speed += 0.1 } }
Overriding properties
class Car: Vehicle { var speed = 0.0 init() { super.init() numOfWheels = 4 } override var description: String { return super.description + ", Speed=\(speed) mph" } }
Struct
struct Point { var x: Double, y: Double } struct Rect { var origin: Point var size: Size //computed property var area: Double { return size.width * size.height } //method func isBiggerThanRect(other: Rect) => Bool { return self.area > other.area } } //init var point = Point(x: 0.0, y: 0.0) var rect = Rect(origin: point, size: size)
Struct and Class
1. struct cannot inherite
2. class is passed by reference. struct is passed by value
Constant and Variables
let window = Window(1.0, 1.0) //The reference is a constant, cannot change the reference value can do window.x = 1.0 cannot do window = Window(2.0, 2.0) var point1 = Point(x: 0.0, y: 0.0) // Point is struct. Since point1 is variable, we can do point1.x = 1.0 let point2 = Point(x: 0.0, y: 0.0) // Cannot do point2.x = 1.0 since point2 is constant
Swift 1.2 conditional assign constant value
let longEdge: CGFloat if isLandscape { longEdge = image.calculateWidth() } else { longEdge = image.calculateHeight() } // you can access longEdge from this point on
Mutating a Structure
struct Point { var x,y: Double //tell the complier this is a mutating function mutating func moveToRightBy(dx: Double) { x += dx } } let point1 = Point(x: 0.0, y: 0.0) point.moveToRightBy(20.0) //Error: Cannot mutate constant
Enums
enum Plannet: Int { case Mecury = 1, Venus case Earth = 5 } let earth = Planet.Earth.toRaw(); enum Direction { case west, east, north, south } var direction = Direction.west direction = .east //example label.testAlignment = .Right
Enumerations: Associated Values
enum FlightStatus { case OnTime case Delayed(Int) // minutes init() { self = OnTime } var desc: String { switch self { case OnTime: return "on time" case Delayed(let minutes): return "deplayed by \(minutes) minute(s)" } } } var status = FlightStatus // equals to var status = FlightStatus.OnTime status = .Delayed(30) status.desc //deplayed by 30 minute(s) //Nested Type class Flight { enum Status { case OnTime, Deplayed(Int) ... } var status = Status.OnTime }
Extensions
extension Size { mutating func scaleByFactor(factor: Int) { width *= factor height *= factor } } extension Int { func repetitions(task: () -> ()) { for i in 0..<self { task() } } } 400.repetitions { println("Hello!") }
Generic Stack Structure (Templates)
struct Stack<T> { var elements = <T>[]() func push {...} func pop {...} } var intStack = Stack<int>() intStack.push(60) let lastIn = intStack.pop()