Top Banner
Swift 101 2014.7.16 Jollen Chen <[email protected]> www.mokoversity.com Mokoversity 1 14年720星期
43

Mokoversity Course: Apple Swift 101 - Introduction

Aug 27, 2014

Download

Software

Jollen Chen

Level 101 introduction to Apple Swift programing language
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 2: Mokoversity Course: Apple Swift 101 - Introduction

Coder to CoderCoder to User

Mokoversity

214年7⽉月20⽇日星期⽇日

Page 3: Mokoversity Course: Apple Swift 101 - Introduction

The quick and not lazy learner

314年7⽉月20⽇日星期⽇日

Page 4: Mokoversity Course: Apple Swift 101 - Introduction

λ414年7⽉月20⽇日星期⽇日

Page 5: Mokoversity Course: Apple Swift 101 - Introduction

(function() {

! 'use strict';

! var name = 'Peter';! var age = 20;

! console.log(name + ' is ' + age + ' years old.');

}) ();

514年7⽉月20⽇日星期⽇日

Page 6: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 7: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 8: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 9: Mokoversity Course: Apple Swift 101 - Introduction

The Swift Programming Languge

VariableConstant

Data TypesControl Flow

Functions and ClosureClass and Object

914年7⽉月20⽇日星期⽇日

Page 10: Mokoversity Course: Apple Swift 101 - Introduction

var age = 20//var height = 175var height: Double = 175.5

let userId = 7533781let 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⽇日星期⽇日

Page 11: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 12: Mokoversity Course: Apple Swift 101 - Introduction

Swift Data Types• number

• explicit type - integer, double and etc

• implicit - number value

• string

• array

Source: https://developer.apple.com/swift/

1214年7⽉月20⽇日星期⽇日

Page 13: Mokoversity Course: Apple Swift 101 - Introduction

EXPERIMENT

Values are never implicitly converted to another type.

let age = 20let note = “The age is “ + String(age)

1314年7⽉月20⽇日星期⽇日

Page 14: Mokoversity Course: Apple Swift 101 - Introduction

Control Flow• if

• switch

• for - in

• for

• whilte

• do - while

Source: https://developer.apple.com/swift/

1414年7⽉月20⽇日星期⽇日

Page 15: Mokoversity Course: Apple Swift 101 - Introduction

var tags = ["marketing", "javascript"]// tags[0] is "marketing"

for tag in tags {! println(tag)}

1514年7⽉月20⽇日星期⽇日

Page 16: Mokoversity Course: Apple Swift 101 - Introduction

var tags = ["marketing", "javascript"]// tags[0] is "marketing"

if tags[0] == "marketing" {! println("There is a marketing tag.")} else {! println("ouch")}

1614年7⽉月20⽇日星期⽇日

Page 17: Mokoversity Course: Apple Swift 101 - Introduction

EXPERIMENT

not an implicit condition, must be explicit expression

let isActive = 1

// cause an errorif isActive {

}

1714年7⽉月20⽇日星期⽇日

Page 18: Mokoversity Course: Apple Swift 101 - Introduction

// 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⽇日星期⽇日

Page 19: Mokoversity Course: Apple Swift 101 - Introduction

let location = "taipei"

switch location {case "taipei":! println("in Taipei")case "tainan":! println("in Tainan")default:! println("unknow city")}

1914年7⽉月20⽇日星期⽇日

Page 20: Mokoversity Course: Apple Swift 101 - Introduction

EXPERIMENT

not an implicit condition, must be explicit default

let location = “taipei”

switch localtion {case “taipei”:...

case “tainan”:...

}

2014年7⽉月20⽇日星期⽇日

Page 21: Mokoversity Course: Apple Swift 101 - Introduction

for var i = 0; i < 3; i++ {! println(i)}

for i in 0...3 {! println(i)}

2114年7⽉月20⽇日星期⽇日

Page 22: Mokoversity Course: Apple Swift 101 - Introduction

Functions• func

• ->

• tuple (return multiple values)

• variable number of arguments

Source: https://developer.apple.com/swift/

2214年7⽉月20⽇日星期⽇日

Page 23: Mokoversity Course: Apple Swift 101 - Introduction

func save(name: String, location:String) -> String {! return "\(name) lives in \(location).";}

println(save("Peter", "Taipei"))

// use a tuplefunc save() -> (Double, Double, Double) {! return (1.1, 1.2, 1.3)}

// variable number of argumentsfunc 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⽇日星期⽇日

Page 24: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 25: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 26: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 27: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 28: Mokoversity Course: Apple Swift 101 - Introduction

// 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⽇日星期⽇日

Page 29: Mokoversity Course: Apple Swift 101 - Introduction

// 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⽇日星期⽇日

Page 30: Mokoversity Course: Apple Swift 101 - Introduction

認識 Anonymous Function• 匿名函數

• function constant

• lambda function

• 發源於 1958 LISP 語⾔言

• 多種語⾔言採⽤用

• 經常使⽤用於 Callback function 參數

• 在 JavaScript 裡,anonymous function 有別於 Closure

3014年7⽉月20⽇日星期⽇日

Page 31: Mokoversity Course: Apple Swift 101 - Introduction

WRITING SAFE CODE

some examples

3114年7⽉月20⽇日星期⽇日

Page 32: Mokoversity Course: Apple Swift 101 - Introduction

Safety Code

“writing safety code”

JavaScript

“design for safety”

Swift / Go

3214年7⽉月20⽇日星期⽇日

Page 33: Mokoversity Course: Apple Swift 101 - Introduction

Data Type

JavaScript Swift / Go

var x;

x = 5; // int...x = {}; // object

x = x - 5;

var x = 5x = "hello"

println(x)

3314年7⽉月20⽇日星期⽇日

Page 34: Mokoversity Course: Apple Swift 101 - Introduction

NULL

C Swift / Go

char *buf;

if (buf == NULL) {}

var buf: NSArray?

3414年7⽉月20⽇日星期⽇日

Page 35: Mokoversity Course: Apple Swift 101 - Introduction

Weak Data Types

Java Swift / Go

class Hello {static int x;int y;}

var x

3514年7⽉月20⽇日星期⽇日

Page 36: Mokoversity Course: Apple Swift 101 - Introduction

Objects and Classes• class

• self

• init / deinit

• override

Source: https://developer.apple.com/swift/

3614年7⽉月20⽇日星期⽇日

Page 37: Mokoversity Course: Apple Swift 101 - Introduction

class Application {! var status = 1

! func getStatus(status: Int) -> String {! ! self.status = status! ! return "Status Code: \(status)"! }}

// create an instancevar application = Application()

// dot syntaxvar status = application.getStatus(5)println(status)

3714年7⽉月20⽇日星期⽇日

Page 38: Mokoversity Course: Apple Swift 101 - Introduction

Enumerations and Structures• enum

• struct

Source: https://developer.apple.com/swift/

3814年7⽉月20⽇日星期⽇日

Page 39: Mokoversity Course: Apple Swift 101 - Introduction

認識 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⽇日星期⽇日

Page 40: Mokoversity Course: Apple Swift 101 - Introduction

Protocol 語法• conform

• 遵守⽅方法宣告 (arguments and return type)

• adopt

• protocol

• mutating

• mark a method that modifies the structure

• extension

4014年7⽉月20⽇日星期⽇日

Page 41: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 42: Mokoversity Course: Apple Swift 101 - Introduction

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⽇日星期⽇日

Page 43: Mokoversity Course: Apple Swift 101 - Introduction

Mokoversity 虛擬進駐計畫敬請提供您的寶貴意⾒見。謝謝!

http://tinyurl.com/neguf4c

4314年7⽉月20⽇日星期⽇日