5. (function() {
! 'use strict';
! var name = 'Peter';
! var age = 20;
! console.log(name + ' is ' + age + ' years old.');
}) ();
514年7⽉月20⽇日星期⽇日
6. Closure (封閉性)
沒有封閉...
var base;
function square( ) {
base = base * base;
}
function() {
var base;
function square( ) {
base = base * base;
}
}
(function() {
var base;
function square( ) {
base = base * base;
}
})
(function() {
var base;
function square( ) {
base = base * base;
}
}) ();
完成封閉,成
為一個封包
614年7⽉月20⽇日星期⽇日
7. Introducing Swift
• Swift is an innovative new programming language
for Cocoa and Cocoa Touch.
• Writing code is interactive and fun, the syntax is
concise yet expressive, and apps run lightning-fast.
• Swift is ready for your next iOS and OS X project —
or for addition into your current app — because
Swift code works side-by-side with Objective-C.
Source: https://developer.apple.com/swift/
714年7⽉月20⽇日星期⽇日
8. Swift Features
• Swift has many other features to make your code
more expressive:
• Closures unified with function pointers
• Tuples and multiple return values
• Generics
• Fast and concise iteration over a range or collection
• Structs that support methods, extensions, protocols.
• Functional programming patterns, e.g.: map and filter
Source: https://developer.apple.com/swift/
814年7⽉月20⽇日星期⽇日
9. The Swift Programming Languge
Variable
Constant
Data Types
Control Flow
Functions and Closure
Class and Object
914年7⽉月20⽇日星期⽇日
10. var age = 20
//var height = 175
var height: Double = 175.5
let userId = 7533781
let fullname = "Peter"
// error
//let note = fullname + " is " + age + " years
old."
//let note = fullname + " is " + String(age) + "
years old."
let note = "(fullname) is (age) years old."
println(note)
1014年7⽉月20⽇日星期⽇日
11. var tags = ["marketing", "javascript"]
// tags[0] is "marketing"
println(tags[0])
var options = [
! "save": "Save to Plain Text",
! "edit": "Edit Text",
]
// options["edit"] is "Edit Text"
println(options["edit"])
1114年7⽉月20⽇日星期⽇日
12. Swift Data Types
• number
• explicit type - integer, double and etc
• implicit - number value
• string
• array
Source: https://developer.apple.com/swift/
1214年7⽉月20⽇日星期⽇日
13. EXPERIMENT
Values are never implicitly converted to
another type.
let age = 20
let note = “The age is “ + String(age)
1314年7⽉月20⽇日星期⽇日
14. Control Flow
• if
• switch
• for - in
• for
• whilte
• do - while
Source: https://developer.apple.com/swift/
1414年7⽉月20⽇日星期⽇日
15. var tags = ["marketing", "javascript"]
// tags[0] is "marketing"
for tag in tags {
! println(tag)
}
1514年7⽉月20⽇日星期⽇日
16. var tags = ["marketing", "javascript"]
// tags[0] is "marketing"
if tags[0] == "marketing" {
! println("There is a marketing tag.")
} else {
! println("ouch")
}
1614年7⽉月20⽇日星期⽇日
17. EXPERIMENT
not an implicit condition, must be explicit
expression
let isActive = 1
// cause an error
if isActive {
}
1714年7⽉月20⽇日星期⽇日
18. // mark the value as optional
// (either a value or nil)
var optionalTag: String? = "swift"
optionalTag = nil
if let tag = optionalTag {
! println(tag)
} else {
! println("nil")
}
1814年7⽉月20⽇日星期⽇日
19. let location = "taipei"
switch location {
case "taipei":
! println("in Taipei")
case "tainan":
! println("in Tainan")
default:
! println("unknow city")
}
1914年7⽉月20⽇日星期⽇日
20. EXPERIMENT
not an implicit condition, must be explicit
default
let location = “taipei”
switch localtion {
case “taipei”:
...
case “tainan”:
...
}
2014年7⽉月20⽇日星期⽇日
21. for var i = 0; i < 3; i++ {
! println(i)
}
for i in 0...3 {
! println(i)
}
2114年7⽉月20⽇日星期⽇日
22. Functions
• func
• ->
• tuple (return multiple values)
• variable number of arguments
Source: https://developer.apple.com/swift/
2214年7⽉月20⽇日星期⽇日
23. func save(name: String, location:String) -> String {
! return "(name) lives in (location).";
}
println(save("Peter", "Taipei"))
// use a tuple
func save() -> (Double, Double, Double) {
! return (1.1, 1.2, 1.3)
}
// variable number of arguments
func sumOf(numbers: Int...) -> Int {
! var sum = 0
! for number in numbers {
! ! sum += number
! }
! return sum
}
println(sumOf(1, 2))
println(sumOf(5, 4, 3, 2, 1))
2314年7⽉月20⽇日星期⽇日
24. From Functions to Closure
• nested function
• first-class type
• return a function
• take another function as one of its agument
• lambda
Source: https://developer.apple.com/swift/
2414年7⽉月20⽇日星期⽇日
25. func sumOf(numbers: Int...) -> Int {
! var sum = 0
! func add(num: Int) {
! ! // access to variables in the outer function
! ! sum = sum + num
! }
! for number in numbers {
! ! add(number)
! }
! return sum
}
println(sumOf(1, 2, 3))
2514年7⽉月20⽇日星期⽇日
26. func makeSum() -> ((Int, Int) -> Int) {
! func sumOf(x: Int, y: Int) -> Int {
! ! return x + y
! }
! return sumOf
}
var fomulate = makeSum()
println(fomulate(10, 5))
2614年7⽉月20⽇日星期⽇日
27. Closure
• Functions re actually a special case of closues.
• You can write a closure without a name by
surrounding code with brances.
• Use in to separate the arguments and return type
from the body.
Source: https://developer.apple.com/swift/
2714年7⽉月20⽇日星期⽇日
28. // named function
func sum(x: Int, y: Int) -> Int {
! return x + y!
}
func makeSum( sum: (Int, Int) -> Int ) -> Int {
! return sum(5, 10)
}
var fomulate = makeSum(sum)
println(fomulate)
2814年7⽉月20⽇日星期⽇日
29. // without a name (closure)
func makeSum( sum: (Int, Int) -> Int ) -> Int {
! return sum(5, 10)
}
var fomulate = makeSum({
! (x: Int, y: Int) -> Int in
! return x + y
})
println(fomulate)
2914年7⽉月20⽇日星期⽇日
30. 認識 Anonymous Function
• 匿名函數
• function constant
• lambda function
• 發源於 1958 LISP 語⾔言
• 多種語⾔言採⽤用
• 經常使⽤用於 Callback function 參數
• 在 JavaScript 裡,anonymous function 有別於
Closure
3014年7⽉月20⽇日星期⽇日
39. 認識 Protocols
• 從程式語⾔言、物件導向、軟體⼯工程、軟體架構等,都有不
同的解釋
• Classes, enumerations, and structures can all adopt
protocols.
• ⼀一份在 class 間分享 method 的清單
• Java 如何實作?Interface 與 Delegation Pattern
• JavaScript 如何實作?Prototype Pattern 與 Function
Object
• 軟體⼯工程?Abstrac Class 與繼承
3914年7⽉月20⽇日星期⽇日
40. Protocol 語法
• conform
• 遵守⽅方法宣告 (arguments and return type)
• adopt
• protocol
• mutating
• mark a method that modifies the structure
• extension
4014年7⽉月20⽇日星期⽇日
41. Swift 的 Data Types 特⾊色
./0000.swift:5:3: error: type 'Int' does not conform to protocol
'StringLiteralConvertible'
x = "hello"
^
var x = 5
x = "hello"
4114年7⽉月20⽇日星期⽇日
42. protocol MyProtocol {
! mutating func add(Int)
}
class SimpleAdder: MyProtocol {
! var sum: Int
! init() {
! ! sum = 0
! }
! func add(x: Int) {
! ! sum = sum + x
! }
! func getSum() -> Int {
! ! return sum
! }
}
var adder = SimpleAdder()
adder.add(5)
adder.add(10)
println(adder.getSum())
4214年7⽉月20⽇日星期⽇日