Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and Swift, MVC

Embed Size (px)

Text of Standford 2015 iOS讀書會 week1: 1.Logistics , iOS 8 Overview 2. More Xcode and Swift, MVC

  • Standford 2015 iOS week1

    1. Logistics , iOS 8 Overview 2. More Xcode and Swift, MVC

  • App:iPhone.iPad

    Swift

  • http://www.facebook.com/iphone.peterpan

    http://deeploveapple.blogspot.tw

    apppeterpan@gmail.com

    https://github.com/AppPeterPan/It-s-all-about-App/wiki/It's-all-about-App

    FB

    blog

    iOSwi

    ki

    http://deeploveiossdk.tumblr.comwiki

    email

    mediumhttps://medium.com/@apppeterpan

    http://swiftbook.strikingly.com

  • Developing iOS 8 Apps with Swift

    iPadiTunes U App

  • Objective-C

    https://medium.com/@apppeterpan/-swift-d1e5091a95c

  • iOS Objective-C

    SwiftiOS7 (97%)

    Swift & Objective-C:

    AppSwift & Objective-C ()

    SwiftObjective-CObjective-CSwift

  • iOS 7 & iOS 8

    https://developer.apple.com/support/appstore/

  • 1.

    2. class, instance, object, method, inheritance, instance variable

    if else, for, variable, function

    Programming Paradigms:

    Programming AbstractionsCS50

  • Xcode 6.3

    Swift 1.2

    Standford VideoXcode 6.1 & Swift 1.1

  • Swift 1.2

  • iOS

  • Framework

  • Demo: App

  • Enter

    Enter

    ()

    ex: (1)12 (2)enter(2)3 (4) - -> 9

  • Storyboard:App

    iPhone ?codeApp

  • frameUI

  • !

  • iPhone

  • auto layout

    auto layoutXcode

    UIauto layout

  • auto layout

    Trailing Space to Container Margin +

  • problem

    : :

  • 2constraint

    label x, y labelsize

    demo: viewx, y & size

  • update frame

  • auto layout

    demo

  • demo: label24constraint

  • preview

  • View Controllerimport UIKit

    class ViewController: UIViewController {

    }

    importmethod

  • IBOutlet: storyboardUI

  • @IBOutlet weak var display: UILabel!

    ! : optional

    propertystored property

  • - Playground

  • Playground

  • var

  • var age = 18

    var age = 18;

    !

    Swift

  • Unicode

    : functionstructenum

    !

  • Quick Look

  • quick looktype

  • Var number = 18

    compile error

  • console

    console

  • console

    compile error

    runtime error ( crash)

    Objective-CNSLog

  • playground

  • demo: storyboardUI

  • method

    override func viewDidLoad() { super.viewDidLoad() }

  • function

  • function

  • function external name & internal name

  • #: external name

  • function

    return: tuple

  • IBAction: storyboard

    @IBAction func appendDigit(sender: UIButton) { }

  • storyboard

  • copy pasteIBAction IBOutlet

    copy paste

  • appendDigit

    @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle println("digit = \(digit)") }

  • (constant)

    18

    let

  • variable & constant

    constant ?

    constant:

  • to var, or not to var, that is the question

    Objective-Cvariable & constant

  • option

  • tab

  • console

    @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle println("digit = \(digit)") }

    Optional

  • string interpolation ()

    ( )

  • type inference let digit = sender.currentTitle

    option()

  • (Type Inference)

    Int

    String

    IntInt

  • Swifttype

  • : Swift

    Objective-C

    30

  • !

  • optional

  • optional optional (Objective-C)

    nil -> nil

    nilvalue

    -1

    -2

  • optional

    optionalnil

    : nil

    var age: Int?

    var age:Int? = nil

    type ? ( )

    typenilObjective-Cnil

  • optional optional

    var age1: Int?

    var age2 = age1

    optionalnil

    optionalcompile error

  • optional

    var number1: Int? = nil

    var number2: Int = nil

    var number3 = nilcompile error

    optional

  • optional

  • optional : !force-unwrap

  • force-unwrap on nilruntime error

  • optional

    nilif

    if , while, for

    1. optional 2. optional

    optionalnil

  • optional binding

  • Swift 1.2: optional binding

  • !: Implicitly Unwrapped Optionals

    ! ?

    unwrapped !

  • nil -> Crash

    !crash

  • if

    optional binding

  • if else true / false

    0truefor , whiletrue / false

  • if else { }

  • if else

    Objective-Cif{ }

  • : !optionalnilcrash

    @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! println("digit = \(digit)") }

  • @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! display.text = display.text! + digit }

  • var userInTheMiddleOfTypingNumber: Bool = false

    var userInTheMiddleOfTypingNumber: Bool?

    initializer

    nil

  • @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! if userInTheMiddleOfTypingNumber { display.text = display.text! + digit } else { display.text = digit userInTheMiddleOfTypingNumber = true } }

  • !optional

    @IBOutlet weak var display: UILabel! var userInTheMiddleOfTypingNumber: Bool = false @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! if userInTheMiddleOfTypingNumber { display.text = display.text! + digit } else { display.text = digit userInTheMiddleOfTypingNumber = true } }

    implicitly unwrapped optional

    IBOutlet storyboard

  • ?optional @IBOutlet weak var display: UILabel? var userInTheMiddleOfTypingNumber: Bool = false @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! if userInTheMiddleOfTypingNumber { display!.text = display!.text! + digit } else { display!.text = digit userInTheMiddleOfTypingNumber = true } }

  • enter

  • problem:

    @IBAction func enter() { userInTheMiddleOfTypingNumber = false }

  • demo IBOutlet / IBAction crash

  • array var operandStack:Array = Array()

    + ( )( )

    var operandStack = Array()

  • array

  • array

  • arraystruct

  • value type & reference type

  • structarray

  • array

  • array

  • array

  • ArrayDouble

  • computed propertyvar displayValue: Double { get { return NSNumberFormatter().numberFromString(display.text!)!.doubleValue } set { display.text = "\(newValue)" userInTheMiddleOfTypingNumber = false } }

  • computed property

  • computed property

  • demo @IBAction func enter() { userInTheMiddleOfTypingNumber = false operandStack.append(displayValue) println("operandStack = \(operandStack)") }

  • @IBAction func operate(sender: UIButton) { }

    : :

  • cmd + /

  • switch@IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userInTheMiddleOfTypingNumber { enter() } switch operation { case "": if operandStack.count >= 2 { displayValue = operandStack.removeLast() * operandStack.removeLast() enter() } case "": if operandStack.count >= 2 { displayValue = operandStack.removeLast() / operandStack.removeLast() enter() } case "+": if operandStack.count >= 2 { displayValue = operandStack.removeLast() + operandStack.removeLast() enter() } case "": if operandStack.count >= 2 { displayValue = operandStack.removeLast() - operandStack.removeLast() enter() } default: break } }

    removeLastcrash

  • switch case

  • switch break

  • switch casefallthrough

    ex: 60 80

  • switch case

  • switch

    Objective-Cswitch

  • better code func performOperation(operation:(Double, Double) -> Double) { if operandStack.count >= 2 { displayValue = operation(operandStack.removeLast(), operandStack.removeLast() ) enter() } } func multiply(op1:Double, op2:Double) -> Double { return op1 * op2 } @IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userInTheMiddleOfTypingNumber { enter() } switch operation { case "": performOperation(multiply)