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

  • Published on
    16-Jul-2015

  • View
    1.007

  • Download
    1

Embed Size (px)

Transcript

<ul><li><p>Standford 2015 iOS week1</p><p>1. Logistics , iOS 8 Overview 2. More Xcode and Swift, MVC</p></li><li><p>App:iPhone.iPad</p><p>Swift</p></li><li><p>http://www.facebook.com/iphone.peterpan</p><p>http://deeploveapple.blogspot.tw</p><p>apppeterpan@gmail.com</p><p>https://github.com/AppPeterPan/It-s-all-about-App/wiki/It's-all-about-App</p><p>FB</p><p>blog</p><p>iOSwi</p><p>ki</p><p>http://deeploveiossdk.tumblr.comwiki</p><p>email</p><p>mediumhttps://medium.com/@apppeterpan</p><p>http://swiftbook.strikingly.com</p></li><li><p>Developing iOS 8 Apps with Swift</p><p>iPadiTunes U App</p></li><li><p>Objective-C</p><p>https://medium.com/@apppeterpan/-swift-d1e5091a95c</p></li><li><p>iOS Objective-C</p><p> SwiftiOS7 (97%) </p><p> Swift &amp; Objective-C: </p><p> AppSwift &amp; Objective-C () </p><p> SwiftObjective-CObjective-CSwift</p></li><li><p>iOS 7 &amp; iOS 8</p><p>https://developer.apple.com/support/appstore/</p></li><li><p>1. </p><p>2. class, instance, object, method, inheritance, instance variable</p><p>if else, for, variable, function</p><p>Programming Paradigms:</p><p>Programming AbstractionsCS50</p></li><li><p>Xcode 6.3</p><p>Swift 1.2</p><p>Standford VideoXcode 6.1 &amp; Swift 1.1</p></li><li><p>Swift 1.2</p></li><li><p>iOS</p></li><li><p> Framework</p></li><li><p>Demo: App</p></li><li><p>Enter</p><p>Enter</p><p>()</p><p>ex: (1)12 (2)enter(2)3 (4) - -&gt; 9</p></li><li><p>Storyboard:App</p><p>iPhone ?codeApp </p></li><li><p>frameUI</p></li><li><p>! </p></li><li><p>iPhone</p></li><li><p>auto layout</p><p> auto layoutXcode </p><p> UIauto layout</p></li><li><p>auto layout</p><p>Trailing Space to Container Margin + </p></li><li><p>problem</p><p>: : </p></li><li><p>2constraint</p><p>label x, y labelsize</p><p>demo: viewx, y &amp; size</p></li><li><p>update frame</p></li><li><p>auto layout</p><p>demo</p></li><li><p>demo: label24constraint</p></li><li><p>preview</p></li><li><p>View Controllerimport UIKit </p><p>class ViewController: UIViewController { </p><p>}</p><p>importmethod</p></li><li><p>IBOutlet: storyboardUI</p></li><li><p> @IBOutlet weak var display: UILabel! </p><p>! : optional</p><p>propertystored property</p></li><li><p> - Playground</p></li><li><p>Playground</p></li><li><p>var</p></li><li><p> var age = 18 </p><p> var age = 18;</p><p>!</p><p>Swift</p></li><li><p>Unicode</p><p>: functionstructenum</p><p> ! </p></li><li><p>Quick Look</p></li><li><p>quick looktype</p></li><li><p> Var number = 18</p><p>compile error</p></li><li><p>console</p><p>console</p></li><li><p>console</p><p> compile error </p><p> runtime error ( crash) </p><p> Objective-CNSLog</p></li><li><p>playground</p></li><li><p>demo: storyboardUI</p></li><li><p>method</p><p>override func viewDidLoad() { super.viewDidLoad() }</p></li><li><p>function</p></li><li><p>function</p></li><li><p>function external name &amp; internal name</p></li><li><p>#: external name</p></li><li><p>function</p><p>return: tuple</p></li><li><p>IBAction: storyboard</p><p>@IBAction func appendDigit(sender: UIButton) { }</p></li><li><p>storyboard </p></li><li><p>copy pasteIBAction IBOutlet</p><p>copy paste</p></li><li><p>appendDigit</p><p>@IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle println("digit = \(digit)") } </p></li><li><p>(constant)</p><p>18 </p><p>let</p></li><li><p>variable &amp; constant</p><p> constant ? </p><p> constant: </p></li><li><p>to var, or not to var, that is the question</p><p>Objective-Cvariable &amp; constant</p></li><li><p>option</p></li><li><p>tab</p></li><li><p>console</p><p>@IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle println("digit = \(digit)") } </p><p>Optional</p></li><li><p>string interpolation ()</p><p>( )</p></li><li><p>type inference let digit = sender.currentTitle </p><p>option()</p></li><li><p>(Type Inference)</p><p>Int</p><p>String</p><p>IntInt</p></li><li><p>Swifttype</p></li><li><p>: Swift</p><p>Objective-C</p><p>30</p></li><li><p> !</p></li><li><p>optional</p></li><li><p>optional optional (Objective-C) </p><p> nil -&gt; nil </p><p> nilvalue </p><p> -1 </p><p> -2</p></li><li><p>optional</p><p>optionalnil</p><p> : nil </p><p> var age: Int? </p><p> var age:Int? = nil </p><p> type ? ( )</p><p> typenilObjective-Cnil</p></li><li><p>optional optional</p><p>var age1: Int? </p><p>var age2 = age1 </p><p>optionalnil</p><p>optionalcompile error</p></li><li><p>optional</p><p>var number1: Int? = nil </p><p>var number2: Int = nil </p><p>var number3 = nilcompile error</p><p>optional</p></li><li><p>optional</p></li><li><p>optional : !force-unwrap</p></li><li><p>force-unwrap on nilruntime error</p></li><li><p>optional</p><p>nilif</p><p>if , while, for</p><p>1. optional 2. optional</p><p>optionalnil</p></li><li><p> optional binding</p></li><li><p>Swift 1.2: optional binding</p></li><li><p>!: Implicitly Unwrapped Optionals </p><p> ! ?</p><p>unwrapped ! </p></li><li><p>nil -&gt; Crash</p><p>!crash</p></li><li><p>if</p><p>optional binding</p></li><li><p>if else true / false</p><p>0truefor , whiletrue / false</p></li><li><p>if else { } </p></li><li><p>if else</p><p>Objective-Cif{ } </p></li><li><p>: !optionalnilcrash</p><p>@IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! println("digit = \(digit)") }</p></li><li><p> @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! display.text = display.text! + digit }</p></li><li><p> var userInTheMiddleOfTypingNumber: Bool = false </p><p> var userInTheMiddleOfTypingNumber: Bool? </p><p>initializer</p><p>nil</p></li><li><p>@IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! if userInTheMiddleOfTypingNumber { display.text = display.text! + digit } else { display.text = digit userInTheMiddleOfTypingNumber = true } } </p></li><li><p>!optional</p><p> @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 } } </p><p>implicitly unwrapped optional</p><p>IBOutlet storyboard </p></li><li><p>?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 } } </p></li><li><p>enter</p></li><li><p>problem: </p><p> @IBAction func enter() { userInTheMiddleOfTypingNumber = false }</p></li><li><p>demo IBOutlet / IBAction crash</p></li><li><p>array var operandStack:Array = Array() </p><p> + ( )( )</p><p> var operandStack = Array() </p></li><li><p>array</p></li><li><p>array</p></li><li><p>arraystruct</p></li><li><p>value type &amp; reference type</p></li><li><p>structarray</p></li><li><p>array</p></li><li><p>array</p></li><li><p>array</p></li><li><p>ArrayDouble</p></li><li><p>computed propertyvar displayValue: Double { get { return NSNumberFormatter().numberFromString(display.text!)!.doubleValue } set { display.text = "\(newValue)" userInTheMiddleOfTypingNumber = false } } </p></li><li><p> computed property</p></li><li><p> computed property</p></li><li><p>demo @IBAction func enter() { userInTheMiddleOfTypingNumber = false operandStack.append(displayValue) println("operandStack = \(operandStack)") } </p></li><li><p> @IBAction func operate(sender: UIButton) { } </p><p>: : </p></li><li><p>cmd + /</p></li><li><p>switch@IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userInTheMiddleOfTypingNumber { enter() } switch operation { case "": if operandStack.count &gt;= 2 { displayValue = operandStack.removeLast() * operandStack.removeLast() enter() } case "": if operandStack.count &gt;= 2 { displayValue = operandStack.removeLast() / operandStack.removeLast() enter() } case "+": if operandStack.count &gt;= 2 { displayValue = operandStack.removeLast() + operandStack.removeLast() enter() } case "": if operandStack.count &gt;= 2 { displayValue = operandStack.removeLast() - operandStack.removeLast() enter() } default: break } } </p><p>removeLastcrash</p></li><li><p>switch case</p></li><li><p>switch break</p></li><li><p>switch casefallthrough</p><p>ex: 60 80</p></li><li><p>switch case</p></li><li><p>switch </p><p>Objective-Cswitch </p></li><li><p>better code func performOperation(operation:(Double, Double) -&gt; Double) { if operandStack.count &gt;= 2 { displayValue = operation(operandStack.removeLast(), operandStack.removeLast() ) enter() } } func multiply(op1:Double, op2:Double) -&gt; Double { return op1 * op2 } @IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userInTheMiddleOfTypingNumber { enter() } switch operation { case "": performOperation(multiply) default: break } } </p><p>function</p></li><li><p>function</p></li><li><p>closure performOperation( (op1:Double, op2:Double) -&gt; Double { return op1 * op2 })</p><p> performOperation( { (op1:Double, op2:Double) -&gt; Double in return op1 * op2 }) </p></li><li><p>closure</p><p> like Objective-Cs block </p><p> function </p><p> function is a special case of closure: </p></li><li><p>functionclosure</p></li><li><p>closure performOperation( { op1, op2 in return op1 * op2 })</p><p> performOperation( { op1, op2 in op1 * op2 })</p><p>return</p></li><li><p>closure performOperation( { $0 * $1 })</p><p> performOperation(){ $0 * $1 }</p><p>function</p></li><li><p>closure performOperation{ $0 * $1 }</p><p>function</p></li><li><p>Best code @IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userInTheMiddleOfTypingNumber { enter() } switch operation { case "": performOperation{ $1 * $0 } case "": performOperation{ $1 / $0 } case "+": performOperation{ $1 + $0 } case "": performOperation{ $1 - $0 } default: break } } </p></li><li><p> func performOperation2(operation:Double -&gt; Double) { if operandStack.count &gt;= 1 { displayValue = operation(operandStack.removeLast()) enter() } }</p><p> case "": performOperation { op1 in sqrt(op1) }</p></li><li><p>Objective-C does not support method overloading, you have to use a dierent method name. When you inherited UIViewController you inherited NSObject and made the class interopable to Obj-C. Swift on the other hand supports overloading, that's why it works when you remove the inheritance</p><p>func performOperation(operation:Double -&gt; Double) { if operandStack.count &gt;= 1 { displayValue = operation(operandStack.removeLast()) enter() } } func performOperation(operation:(Double, Double) -&gt; Double) { if operandStack.count &gt;= 2 { displayValue = operation(operandStack.removeLast(), operandStack.removeLast() ) enter() } } </p></li><li><p>performOperation { sqrt($0) }</p></li><li><p>auto layout</p></li><li><p>Update Frame</p></li><li><p>Update Frame</p></li><li><p>Demo</p></li><li><p>Clear Constraint</p></li><li><p>MVC</p></li><li><p>MVC</p></li><li><p>The Swift Programming Language</p></li></ul>