Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Swift 101
2014.7.16
Jollen Chen
<jollen@jollen.org>
www.mokoversity.com
Mokoversity
114年7⽉月20⽇日星期⽇日
Coder to Coder
Coder to User
Mokoversity
214年7⽉月20⽇日星期⽇日
The quick and
not lazy learner
314年7⽉月20⽇日星期⽇日
λ
414年7⽉月20⽇日星期⽇日
(function() {
! 'use strict';
! var name = 'Peter';
! var age = 20;
! console.log(name + ' is ' + age + ' years old.');
})...
Closure (封閉性)
沒有封閉...
var base;
function square( ) {
base = base * base;
}
function() {
var base;
function square( ) {
bas...
Introducing Swift
• Swift is an innovative new programming language
for Cocoa and Cocoa Touch.
• Writing code is interacti...
Swift Features
• Swift has many other features to make your code
more expressive:
• Closures unified with function pointers...
The Swift Programming Languge
Variable
Constant
Data Types
Control Flow
Functions and Closure
Class and Object
914年7⽉月20⽇日...
var age = 20
//var height = 175
var height: Double = 175.5
let userId = 7533781
let fullname = "Peter"
// error
//let note...
var tags = ["marketing", "javascript"]
// tags[0] is "marketing"
println(tags[0])
var options = [
! "save": "Save to Plain...
Swift Data Types
• number
• explicit type - integer, double and etc
• implicit - number value
• string
• array
Source: htt...
EXPERIMENT
Values are never implicitly converted to
another type.
let age = 20
let note = “The age is “ + String(age)
1314...
Control Flow
• if
• switch
• for - in
• for
• whilte
• do - while
Source: https://developer.apple.com/swift/
1414年7⽉月20⽇日星...
var tags = ["marketing", "javascript"]
// tags[0] is "marketing"
for tag in tags {
! println(tag)
}
1514年7⽉月20⽇日星期⽇日
var tags = ["marketing", "javascript"]
// tags[0] is "marketing"
if tags[0] == "marketing" {
! println("There is a marketi...
EXPERIMENT
not an implicit condition, must be explicit
expression
let isActive = 1
// cause an error
if isActive {
}
1714年...
// mark the value as optional
// (either a value or nil)
var optionalTag: String? = "swift"
optionalTag = nil
if let tag =...
let location = "taipei"
switch location {
case "taipei":
! println("in Taipei")
case "tainan":
! println("in Tainan")
defa...
EXPERIMENT
not an implicit condition, must be explicit
default
let location = “taipei”
switch localtion {
case “taipei”:
....
for var i = 0; i < 3; i++ {
! println(i)
}
for i in 0...3 {
! println(i)
}
2114年7⽉月20⽇日星期⽇日
Functions
• func
• ->
• tuple (return multiple values)
• variable number of arguments
Source: https://developer.apple.com/...
func save(name: String, location:String) -> String {
! return "(name) lives in (location).";
}
println(save("Peter", "Taip...
From Functions to Closure
• nested function
• first-class type
• return a function
• take another function as one of its ag...
func sumOf(numbers: Int...) -> Int {
! var sum = 0
! func add(num: Int) {
! ! // access to variables in the outer function...
func makeSum() -> ((Int, Int) -> Int) {
! func sumOf(x: Int, y: Int) -> Int {
! ! return x + y
! }
! return sumOf
}
var fo...
Closure
• Functions re actually a special case of closues.
• You can write a closure without a name by
surrounding code wi...
// named function
func sum(x: Int, y: Int) -> Int {
! return x + y!
}
func makeSum( sum: (Int, Int) -> Int ) -> Int {
! re...
// without a name (closure)
func makeSum( sum: (Int, Int) -> Int ) -> Int {
! return sum(5, 10)
}
var fomulate = makeSum({...
認識 Anonymous Function
• 匿名函數
• function constant
• lambda function
• 發源於 1958 LISP 語⾔言
• 多種語⾔言採⽤用
• 經常使⽤用於 Callback functi...
WRITING SAFE CODE
some examples
3114年7⽉月20⽇日星期⽇日
Safety Code
writing safety
code
JavaScript
design for
safety
Swift / Go
3214年7⽉月20⽇日星期⽇日
Data Type
JavaScript Swift / Go
var x;
x = 5; // int
.
.
.
x = {}; // object
x = x - 5;
var x = 5
x = "hello"
println(x)
3...
NULL
C Swift / Go
char *buf;
if (buf == NULL) {
}
var buf: NSArray?
3414年7⽉月20⽇日星期⽇日
Weak Data Types
Java Swift / Go
class Hello {
static int x;
int y;
}
var x
3514年7⽉月20⽇日星期⽇日
Objects and Classes
• class
• self
• init / deinit
• override
Source: https://developer.apple.com/swift/
3614年7⽉月20⽇日星期⽇日
class Application {
! var status = 1
! func getStatus(status: Int) -> String {
! ! self.status = status
! ! return "Status...
Enumerations and Structures
• enum
• struct
Source: https://developer.apple.com/swift/
3814年7⽉月20⽇日星期⽇日
認識 Protocols
• 從程式語⾔言、物件導向、軟體⼯工程、軟體架構等,都有不
同的解釋
• Classes, enumerations, and structures can all adopt
protocols.
• ⼀一份在 cl...
Protocol 語法
• conform
• 遵守⽅方法宣告 (arguments and return type)
• adopt
• protocol
• mutating
• mark a method that modifies the...
Swift 的 Data Types 特⾊色
./0000.swift:5:3: error: type 'Int' does not conform to protocol
'StringLiteralConvertible'
x = "he...
protocol MyProtocol {
! mutating func add(Int)
}
class SimpleAdder: MyProtocol {
! var sum: Int
! init() {
! ! sum = 0
! }...
Mokoversity 虛擬進駐計畫
敬請提供您的寶貴意⾒見。謝謝!
http://tinyurl.com/neguf4c
4314年7⽉月20⽇日星期⽇日
Upcoming SlideShare
Loading in …5
×

Mokoversity Course: Apple Swift 101 - Introduction

1,272 views

Published on

Level 101 introduction to Apple Swift programing language

Published in: Software

Mokoversity Course: Apple Swift 101 - Introduction

  1. 1. Swift 101 2014.7.16 Jollen Chen <jollen@jollen.org> www.mokoversity.com Mokoversity 114年7⽉月20⽇日星期⽇日
  2. 2. Coder to Coder Coder to User Mokoversity 214年7⽉月20⽇日星期⽇日
  3. 3. The quick and not lazy learner 314年7⽉月20⽇日星期⽇日
  4. 4. λ 414年7⽉月20⽇日星期⽇日
  5. 5. (function() { ! 'use strict'; ! var name = 'Peter'; ! var age = 20; ! console.log(name + ' is ' + age + ' years old.'); }) (); 514年7⽉月20⽇日星期⽇日
  6. 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. 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. 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. 9. The Swift Programming Languge Variable Constant Data Types Control Flow Functions and Closure Class and Object 914年7⽉月20⽇日星期⽇日
  10. 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. 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. 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. 13. EXPERIMENT Values are never implicitly converted to another type. let age = 20 let note = “The age is “ + String(age) 1314年7⽉月20⽇日星期⽇日
  14. 14. Control Flow • if • switch • for - in • for • whilte • do - while Source: https://developer.apple.com/swift/ 1414年7⽉月20⽇日星期⽇日
  15. 15. var tags = ["marketing", "javascript"] // tags[0] is "marketing" for tag in tags { ! println(tag) } 1514年7⽉月20⽇日星期⽇日
  16. 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. 17. EXPERIMENT not an implicit condition, must be explicit expression let isActive = 1 // cause an error if isActive { } 1714年7⽉月20⽇日星期⽇日
  18. 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. 19. let location = "taipei" switch location { case "taipei": ! println("in Taipei") case "tainan": ! println("in Tainan") default: ! println("unknow city") } 1914年7⽉月20⽇日星期⽇日
  20. 20. EXPERIMENT not an implicit condition, must be explicit default let location = “taipei” switch localtion { case “taipei”: ... case “tainan”: ... } 2014年7⽉月20⽇日星期⽇日
  21. 21. for var i = 0; i < 3; i++ { ! println(i) } for i in 0...3 { ! println(i) } 2114年7⽉月20⽇日星期⽇日
  22. 22. Functions • func • -> • tuple (return multiple values) • variable number of arguments Source: https://developer.apple.com/swift/ 2214年7⽉月20⽇日星期⽇日
  23. 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. 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. 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. 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. 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. 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. 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. 30. 認識 Anonymous Function • 匿名函數 • function constant • lambda function • 發源於 1958 LISP 語⾔言 • 多種語⾔言採⽤用 • 經常使⽤用於 Callback function 參數 • 在 JavaScript 裡,anonymous function 有別於 Closure 3014年7⽉月20⽇日星期⽇日
  31. 31. WRITING SAFE CODE some examples 3114年7⽉月20⽇日星期⽇日
  32. 32. Safety Code writing safety code JavaScript design for safety Swift / Go 3214年7⽉月20⽇日星期⽇日
  33. 33. Data Type JavaScript Swift / Go var x; x = 5; // int . . . x = {}; // object x = x - 5; var x = 5 x = "hello" println(x) 3314年7⽉月20⽇日星期⽇日
  34. 34. NULL C Swift / Go char *buf; if (buf == NULL) { } var buf: NSArray? 3414年7⽉月20⽇日星期⽇日
  35. 35. Weak Data Types Java Swift / Go class Hello { static int x; int y; } var x 3514年7⽉月20⽇日星期⽇日
  36. 36. Objects and Classes • class • self • init / deinit • override Source: https://developer.apple.com/swift/ 3614年7⽉月20⽇日星期⽇日
  37. 37. class Application { ! var status = 1 ! func getStatus(status: Int) -> String { ! ! self.status = status ! ! return "Status Code: (status)" ! } } // create an instance var application = Application() // dot syntax var status = application.getStatus(5) println(status) 3714年7⽉月20⽇日星期⽇日
  38. 38. Enumerations and Structures • enum • struct Source: https://developer.apple.com/swift/ 3814年7⽉月20⽇日星期⽇日
  39. 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. 40. Protocol 語法 • conform • 遵守⽅方法宣告 (arguments and return type) • adopt • protocol • mutating • mark a method that modifies the structure • extension 4014年7⽉月20⽇日星期⽇日
  41. 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. 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⽇日星期⽇日
  43. 43. Mokoversity 虛擬進駐計畫 敬請提供您的寶貴意⾒見。謝謝! http://tinyurl.com/neguf4c 4314年7⽉月20⽇日星期⽇日

×