Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SWIFT1 Optional

Swift tutorial for beginner.
Topics: Optional

  • Be the first to comment

  • Be the first to like this

SWIFT1 Optional

  1. 1. Swift
  2. 2. Java < Scala < Swift Java + Eclipse = COBOL Scala + Idea = cool but tricky Swift + Xcode = easy and fun
  3. 3. Let’s study Swift. Swift We r gonna study Swift with Playground.
  4. 4. Hello World var str: String = “SWIFT” println(“Hello (str) World”)
  5. 5. Type Inference var list: String = “Hello World” var list = “Hello World” var obj: MyClass = MyClass() var obj = MyClass() noun: Inference verb: Infer reference report
  6. 6. ? var value: Optional<Int> var value: Int? ? indicates that the value may contain nil. func getValueThisMayReturnNil() -> String? func getValueThisWillNotReturnNil() -> String var ret = getValueThisWillNotReturnNil() if ret != nil { // this does not make sense! ret.doSometing()
  7. 7. func getValueThisMayReturnNil() -> String? if let ret = getValueThisMayReturnNil() // Unwrappe if Some { ret.doSometing() // normal } else { // Nil } https://www.youtube.com/watch?v=KSkPE_-gBkc&index=4&list=PLy7oRd3ashWodnpf8rjfYEkTgwbOEsKfU&spfreload=1
  8. 8. ! var value: String? How to get a content of the Optional value? var ret = value! // get var ret = value ?? “It is nil” // getOrElse
  9. 9. Exceptions Suck We are really sick and tired of Java Exceptions. Exceptions remind me of GO TO statement. try { int val = String(“xxx”).parseInt(); doSomething…; doSomething…; doSomething…; doSomething…; doSomething…; doSomething…; } catch(Exception e) { // What has happened ??? // i don’t care…. just throws e; }

×