Mokoversity Course: Apple Swift 101 - Introduction

  • View
    331

  • Download
    1

Embed Size (px)

DESCRIPTION

Level 101 introduction to Apple Swift programing language

Transcript

  • Swift 101 2014.7.16 Jollen Chen www.mokoversity.com Mokoversity 114720
  • Coder to Coder Coder to User Mokoversity 214720
  • The quick and not lazy learner 314720
  • 414720
  • (function() { ! 'use strict'; ! var name = 'Peter'; ! var age = 20; ! console.log(name + ' is ' + age + ' years old.'); }) (); 514720
  • 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; } }) (); 614720
  • 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/ 714720
  • Swift Features Swift has many other features to make your code more expressive: Closures unied 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 lter Source: https://developer.apple.com/swift/ 814720
  • The Swift Programming Languge Variable Constant Data Types Control Flow Functions and Closure Class and Object 914720
  • 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) 1014720
  • 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"]) 1114720
  • Swift Data Types number explicit type - integer, double and etc implicit - number value string array Source: https://developer.apple.com/swift/ 1214720
  • EXPERIMENT Values are never implicitly converted to another type. let age = 20 let note = The age is + String(age) 1314720
  • Control Flow if switch for - in for whilte do - while Source: https://developer.apple.com/swift/ 1414720
  • var tags = ["marketing", "javascript"] // tags[0] is "marketing" for tag in tags { ! println(tag) } 1514720
  • var tags = ["marketing", "javascript"] // tags[0] is "marketing" if tags[0] == "marketing" { ! println("There is a marketing tag.") } else { ! println("ouch") } 1614720
  • EXPERIMENT not an implicit condition, must be explicit expression let isActive = 1 // cause an error if isActive { } 1714720
  • // 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") } 1814720
  • let location = "taipei" switch location { case "taipei": ! println("in Taipei") case "tainan": ! println("in Tainan") default: ! println("unknow city") } 1914720
  • EXPERIMENT not an implicit condition, must be explicit default let location = taipei switch localtion { case taipei: ... case tainan: ... } 2014720
  • for var i = 0; i < 3; i++ { ! println(i) } for i in 0...3 { ! println(i) } 2114720
  • Functions func -> tuple (return multiple values) variable number of arguments Source: https://developer.apple.com/swift/ 2214720
  • 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)) 2314720
  • From Functions to Closure nested function rst-class type return a function take another function as one of its agument lambda Source: https://developer.apple.com/swift/ 2414720
  • 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)) 2514720
  • func makeSum() -> ((Int, Int) -> Int) { ! func sumOf(x: Int, y: Int) -> Int { ! ! return x + y ! } ! return sumOf } var fomulate = makeSum() println(fomulate(10, 5)) 2614720
  • 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/ 2714720
  • // 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) 2814720
  • // 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) 2914720
  • Anonymous Function function constant lambda function 1958 LISP Callback function JavaScript anonymous function Closure 3014720
  • WRITING SAFE CODE some examples 3114720
  • Safety Code writing safety code JavaScript design for safety Swift / Go 3214720
  • Data Type JavaScript Swift / Go var x; x = 5; // int . . . x = {}; // object x = x - 5; var x = 5 x = "hello" println(x) 3314720
  • NULL C Swift / Go char *buf; if (buf == NULL) { } var buf: NSArray? 3414720
  • Weak Data Types Java Swift / Go class Hello { static int x; int y; } var x 3514720
  • Objects and Classes class self init / deinit override Source: https://developer.apple.com/swift/ 3614720
  • 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) 3714720
  • Enumerations and Structures enum struct Source: https://developer.apple.com/swift/ 3814720
  • Protocols Classes, enumerations, and structures can all adopt protocols. class method Java Interface Delegation Pattern JavaScript Prototype Pattern Function Object Abstrac Class 3914720
  • Protocol conform (arguments and return type) adopt protocol mutating mark a method that modies the structure extension 4014720
  • Swift Data Types ./0000.swift:5:3: error: type 'Int' does not conform to protocol 'StringLiteralConvertible' x = "hello" ^ var x = 5 x = "hello" 4114720
  • 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()) 4214720
  • Mokoversity http://tinyurl.com/neguf4c 4314720