Upload
jollen-chen
View
409
Download
1
Embed Size (px)
DESCRIPTION
Level 101 introduction to Apple Swift programing language
Citation preview
Coder to CoderCoder 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
VariableConstant
Data TypesControl Flow
Functions and ClosureClass and Object
914年7⽉月20⽇日星期⽇日
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⽇日星期⽇日
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 = 20let 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 errorif 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 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⽇日星期⽇日
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 = 5x = "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 instancevar application = Application()
// dot syntaxvar 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⽇日星期⽇日