Difference between revisions of "Swift Introduction"
From Hawk Wiki
(print) |
|||
Line 1: | Line 1: | ||
− | + | =Swift Basic= | |
− | + | ==Var== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
var languageName: String = "Swift" | var languageName: String = "Swift" | ||
</pre> | </pre> | ||
− | + | ==Constant== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
let version: Double = 1.0 | let version: Double = 1.0 | ||
Line 13: | Line 13: | ||
</pre> | </pre> | ||
− | + | ==Examples== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
let a = 3, b = 5 | let a = 3, b = 5 | ||
Line 23: | Line 23: | ||
let ab = a + b | let ab = a + b | ||
</pre> | </pre> | ||
− | + | ==Array== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
var names = ["Anna", "Alex", 42] | var names = ["Anna", "Alex", 42] | ||
Line 40: | Line 40: | ||
numOfLegs["spider"] = 8 | numOfLegs["spider"] = 8 | ||
</pre> | </pre> | ||
− | + | ==Optionals== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
let possibleLegs: Int? = num | let possibleLegs: Int? = num | ||
Line 66: | Line 66: | ||
</pre> | </pre> | ||
− | + | ==Tuples== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
(404, "Not found") | (404, "Not found") | ||
Line 72: | Line 72: | ||
let (statusCode, message) = refresh() | let (statusCode, message) = refresh() | ||
</pre> | </pre> | ||
− | + | ==Closures== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
func repeat(count: Int, task: () -> ()) { | func repeat(count: Int, task: () -> ()) { | ||
Line 86: | Line 86: | ||
} | } | ||
</pre> | </pre> | ||
− | + | ==Class== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
class Vehicle { | class Vehicle { | ||
Line 115: | Line 115: | ||
} | } | ||
</pre> | </pre> | ||
− | + | ==Struct== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
struct Rect { | struct Rect { | ||
Line 129: | Line 129: | ||
</pre> | </pre> | ||
− | + | ==Struct and Class== | |
1. struct cannot inherite<br> | 1. struct cannot inherite<br> | ||
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== | |
<pre class="brush:c"> | <pre class="brush:c"> | ||
let window = Window(1.0, 1.0) | let window = Window(1.0, 1.0) |
Revision as of 19:58, 26 January 2015
Contents
Swift Basic
Var
var languageName: String = "Swift"
Constant
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
var names = ["Anna", "Alex", 42] var names: String[] = ["Anna", "Alex"] var numOfLegs = ["ant": 6, "snake": 0] 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
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)") }
Tuples
(404, "Not found") (2, "Banana", 0.42) let (statusCode, message) = refresh()
Closures
func repeat(count: Int, task: () -> ()) { for i in 0..<count { task() } } repeat(2, { println("hello") }) repeat(2) { println("hello") }
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 } override var speed: Double { willSet { if newValue > 65.0 { println('warning too fast') } } didSet { //oldValue available } } }
Struct
struct Rect { var origin: Point var size: Size var area: Double { return size.width * size.height } func isBiggerThanRect(other: Rect) => Bool { return self.area > other.area } }
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. can do point1.x = 1.0 let point2 = Point(x: 0.0, y: 0.0) // cannot do point2.x = 1.0