SlideShare a Scribd company logo
1 of 43
Download to read offline
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.');
}) ();
514年7⽉月20⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
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 = 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⽇日星期⽇日
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⽇日星期⽇日
Swift Data Types
• number
• explicit type - integer, double and etc
• implicit - number value
• string
• array
Source: https://developer.apple.com/swift/
1214年7⽉月20⽇日星期⽇日
EXPERIMENT
Values are never implicitly converted to
another type.
let age = 20
let note = “The age is “ + String(age)
1314年7⽉月20⽇日星期⽇日
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 marketing tag.")
} else {
! println("ouch")
}
1614年7⽉月20⽇日星期⽇日
EXPERIMENT
not an implicit condition, must be explicit
expression
let isActive = 1
// cause an error
if isActive {
}
1714年7⽉月20⽇日星期⽇日
// 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⽇日星期⽇日
let location = "taipei"
switch location {
case "taipei":
! println("in Taipei")
case "tainan":
! println("in Tainan")
default:
! println("unknow city")
}
1914年7⽉月20⽇日星期⽇日
EXPERIMENT
not an implicit condition, must be explicit
default
let location = “taipei”
switch localtion {
case “taipei”:
...
case “tainan”:
...
}
2014年7⽉月20⽇日星期⽇日
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/swift/
2214年7⽉月20⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
// 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⽇日星期⽇日
// 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⽇日星期⽇日
認識 Anonymous Function
• 匿名函數
• function constant
• lambda function
• 發源於 1958 LISP 語⾔言
• 多種語⾔言採⽤用
• 經常使⽤用於 Callback function 參數
• 在 JavaScript 裡,anonymous function 有別於
Closure
3014年7⽉月20⽇日星期⽇日
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)
3314年7⽉月20⽇日星期⽇日
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 Code: (status)"
! }
}
// create an instance
var application = Application()
// dot syntax
var status = application.getStatus(5)
println(status)
3714年7⽉月20⽇日星期⽇日
Enumerations and Structures
• enum
• struct
Source: https://developer.apple.com/swift/
3814年7⽉月20⽇日星期⽇日
認識 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⽇日星期⽇日
Protocol 語法
• conform
• 遵守⽅方法宣告 (arguments and return type)
• adopt
• protocol
• mutating
• mark a method that modifies the structure
• extension
4014年7⽉月20⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
Mokoversity 虛擬進駐計畫
敬請提供您的寶貴意⾒見。謝謝!
http://tinyurl.com/neguf4c
4314年7⽉月20⽇日星期⽇日

More Related Content

What's hot

千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7Justin Lin
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算建興 王
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Trainingbeijing.josh
 
Introduction to C++ over CLI
Introduction to C++ over CLIIntroduction to C++ over CLI
Introduction to C++ over CLI建興 王
 
Java 8 與 retrolambda
Java 8 與 retrolambdaJava 8 與 retrolambda
Java 8 與 retrolambdaJustin Lin
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointerschchwy Chang
 
如何用JDK8實作一個小型的關聯式資料庫系統
如何用JDK8實作一個小型的關聯式資料庫系統如何用JDK8實作一個小型的關聯式資料庫系統
如何用JDK8實作一個小型的關聯式資料庫系統なおき きしだ
 
C++11综述/新特性描述/Overview of C++11 New Features
C++11综述/新特性描述/Overview of C++11 New FeaturesC++11综述/新特性描述/Overview of C++11 New Features
C++11综述/新特性描述/Overview of C++11 New FeaturesPeien Luo
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Justin Lin
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7Justin Lin
 

What's hot (11)

千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
 
Introduction to C++ over CLI
Introduction to C++ over CLIIntroduction to C++ over CLI
Introduction to C++ over CLI
 
Java 8 與 retrolambda
Java 8 與 retrolambdaJava 8 與 retrolambda
Java 8 與 retrolambda
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
 
Execution
ExecutionExecution
Execution
 
如何用JDK8實作一個小型的關聯式資料庫系統
如何用JDK8實作一個小型的關聯式資料庫系統如何用JDK8實作一個小型的關聯式資料庫系統
如何用JDK8實作一個小型的關聯式資料庫系統
 
C++11综述/新特性描述/Overview of C++11 New Features
C++11综述/新特性描述/Overview of C++11 New FeaturesC++11综述/新特性描述/Overview of C++11 New Features
C++11综述/新特性描述/Overview of C++11 New Features
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
 
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
 

Similar to Mokoversity Course: Apple Swift 101 - Introduction

105-2 iOS程式設計(二)
105-2 iOS程式設計(二) 105-2 iOS程式設計(二)
105-2 iOS程式設計(二) Hao Lee
 
Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]yiditushe
 
12, string
12, string12, string
12, stringted-xu
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdfczzz1
 
JavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsJavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsHo Kim
 
Js的国(转载)
Js的国(转载)Js的国(转载)
Js的国(转载)Leo Hui
 
千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7javatwo2011
 
揭开Javascript的面纱
揭开Javascript的面纱揭开Javascript的面纱
揭开Javascript的面纱qiang
 
Java8 lambda
Java8 lambdaJava8 lambda
Java8 lambdakoji lin
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introductionotakustay
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013Michael Pan
 
⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨Wen-Tien Chang
 
Clojure简介与应用
Clojure简介与应用Clojure简介与应用
Clojure简介与应用Robert Hao
 
一拍一产品背后的故事(React实战)
一拍一产品背后的故事(React实战)一拍一产品背后的故事(React实战)
一拍一产品背后的故事(React实战)Kejun Zhang
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
Spock:願你的測試長長久久、生生不息
Spock:願你的測試長長久久、生生不息Spock:願你的測試長長久久、生生不息
Spock:願你的測試長長久久、生生不息Shihpeng Lin
 

Similar to Mokoversity Course: Apple Swift 101 - Introduction (20)

105-2 iOS程式設計(二)
105-2 iOS程式設計(二) 105-2 iOS程式設計(二)
105-2 iOS程式設計(二)
 
Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]Java Jdk6学习笔记[Ppt]
Java Jdk6学习笔记[Ppt]
 
12, string
12, string12, string
12, string
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdf
 
Golang
GolangGolang
Golang
 
JavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization SkillsJavaScript 80+ Programming and Optimization Skills
JavaScript 80+ Programming and Optimization Skills
 
Js的国(转载)
Js的国(转载)Js的国(转载)
Js的国(转载)
 
千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7千呼萬喚始出來的Java SE 7
千呼萬喚始出來的Java SE 7
 
揭开Javascript的面纱
揭开Javascript的面纱揭开Javascript的面纱
揭开Javascript的面纱
 
Java8 lambda
Java8 lambdaJava8 lambda
Java8 lambda
 
Dev307
Dev307Dev307
Dev307
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introduction
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013
 
⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨
 
Clojure简介与应用
Clojure简介与应用Clojure简介与应用
Clojure简介与应用
 
一拍一产品背后的故事(React实战)
一拍一产品背后的故事(React实战)一拍一产品背后的故事(React实战)
一拍一产品背后的故事(React实战)
 
ios分享
ios分享ios分享
ios分享
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
Spock:願你的測試長長久久、生生不息
Spock:願你的測試長長久久、生生不息Spock:願你的測試長長久久、生生不息
Spock:願你的測試長長久久、生生不息
 

More from Jollen Chen

Flowchain blockchain classroom at Taiwan Tech University
Flowchain blockchain classroom at Taiwan Tech UniversityFlowchain blockchain classroom at Taiwan Tech University
Flowchain blockchain classroom at Taiwan Tech UniversityJollen Chen
 
Bitmark and Hyperledger Workshop: the Digital Assets and Property
Bitmark and Hyperledger Workshop: the Digital Assets and PropertyBitmark and Hyperledger Workshop: the Digital Assets and Property
Bitmark and Hyperledger Workshop: the Digital Assets and PropertyJollen Chen
 
Introducing the Blockchain and Distributed Ledger Technology
Introducing the Blockchain and  Distributed Ledger TechnologyIntroducing the Blockchain and  Distributed Ledger Technology
Introducing the Blockchain and Distributed Ledger TechnologyJollen Chen
 
Maker of Things - the open IoT cloud for makers chapter.
Maker of Things - the open IoT cloud for makers chapter.Maker of Things - the open IoT cloud for makers chapter.
Maker of Things - the open IoT cloud for makers chapter.Jollen Chen
 
WoT.City and IoT Protocols Movement @ Taipei, Taiwan
WoT.City and IoT Protocols Movement @ Taipei, TaiwanWoT.City and IoT Protocols Movement @ Taipei, Taiwan
WoT.City and IoT Protocols Movement @ Taipei, TaiwanJollen Chen
 
IoT and Maker Crossover (IMCO) Conference 2015
IoT and Maker Crossover (IMCO) Conference 2015IoT and Maker Crossover (IMCO) Conference 2015
IoT and Maker Crossover (IMCO) Conference 2015Jollen Chen
 
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.Jollen Chen
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101Jollen Chen
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Jollen Chen
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101Jollen Chen
 
Android Wear SDK: Level 101
Android Wear SDK: Level 101Android Wear SDK: Level 101
Android Wear SDK: Level 101Jollen Chen
 
Startup eng-camp 3
Startup eng-camp 3Startup eng-camp 3
Startup eng-camp 3Jollen Chen
 
讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV FrameworkJollen Chen
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)Jollen Chen
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)Jollen Chen
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)Jollen Chen
 
Android HAL Introduction: libhardware and its legacy
Android HAL Introduction: libhardware and its legacyAndroid HAL Introduction: libhardware and its legacy
Android HAL Introduction: libhardware and its legacyJollen Chen
 
Jollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-levelJollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-levelJollen Chen
 
Embedded Linux: Introduction
Embedded Linux: IntroductionEmbedded Linux: Introduction
Embedded Linux: IntroductionJollen Chen
 
Android Application: Introduction
Android Application: IntroductionAndroid Application: Introduction
Android Application: IntroductionJollen Chen
 

More from Jollen Chen (20)

Flowchain blockchain classroom at Taiwan Tech University
Flowchain blockchain classroom at Taiwan Tech UniversityFlowchain blockchain classroom at Taiwan Tech University
Flowchain blockchain classroom at Taiwan Tech University
 
Bitmark and Hyperledger Workshop: the Digital Assets and Property
Bitmark and Hyperledger Workshop: the Digital Assets and PropertyBitmark and Hyperledger Workshop: the Digital Assets and Property
Bitmark and Hyperledger Workshop: the Digital Assets and Property
 
Introducing the Blockchain and Distributed Ledger Technology
Introducing the Blockchain and  Distributed Ledger TechnologyIntroducing the Blockchain and  Distributed Ledger Technology
Introducing the Blockchain and Distributed Ledger Technology
 
Maker of Things - the open IoT cloud for makers chapter.
Maker of Things - the open IoT cloud for makers chapter.Maker of Things - the open IoT cloud for makers chapter.
Maker of Things - the open IoT cloud for makers chapter.
 
WoT.City and IoT Protocols Movement @ Taipei, Taiwan
WoT.City and IoT Protocols Movement @ Taipei, TaiwanWoT.City and IoT Protocols Movement @ Taipei, Taiwan
WoT.City and IoT Protocols Movement @ Taipei, Taiwan
 
IoT and Maker Crossover (IMCO) Conference 2015
IoT and Maker Crossover (IMCO) Conference 2015IoT and Maker Crossover (IMCO) Conference 2015
IoT and Maker Crossover (IMCO) Conference 2015
 
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101
 
Android Wear SDK: Level 101
Android Wear SDK: Level 101Android Wear SDK: Level 101
Android Wear SDK: Level 101
 
Startup eng-camp 3
Startup eng-camp 3Startup eng-camp 3
Startup eng-camp 3
 
讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(2)
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(1)
 
Android HAL Introduction: libhardware and its legacy
Android HAL Introduction: libhardware and its legacyAndroid HAL Introduction: libhardware and its legacy
Android HAL Introduction: libhardware and its legacy
 
Jollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-levelJollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-level
 
Embedded Linux: Introduction
Embedded Linux: IntroductionEmbedded Linux: Introduction
Embedded Linux: Introduction
 
Android Application: Introduction
Android Application: IntroductionAndroid Application: Introduction
Android Application: Introduction
 

Mokoversity Course: Apple Swift 101 - Introduction

  • 2. Coder to Coder Coder to User Mokoversity 214年7⽉月20⽇日星期⽇日
  • 3. The quick and not lazy learner 314年7⽉月20⽇日星期⽇日
  • 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⽇日星期⽇日
  • 31. WRITING SAFE CODE some examples 3114年7⽉月20⽇日星期⽇日
  • 32. Safety Code writing safety code JavaScript design for safety Swift / Go 3214年7⽉月20⽇日星期⽇日
  • 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. NULL C Swift / Go char *buf; if (buf == NULL) { } var buf: NSArray? 3414年7⽉月20⽇日星期⽇日
  • 35. Weak Data Types Java Swift / Go class Hello { static int x; int y; } var x 3514年7⽉月20⽇日星期⽇日
  • 36. Objects and Classes • class • self • init / deinit • override Source: https://developer.apple.com/swift/ 3614年7⽉月20⽇日星期⽇日
  • 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. Enumerations and Structures • enum • struct Source: https://developer.apple.com/swift/ 3814年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⽇日星期⽇日