Swift
Java < Scala < Swift
Java + Eclipse = COBOL
Scala + Idea = cool but tricky
Swift + Xcode = easy and fun
Let’s study Swift.
Swift
We r gonna study Swift with Playground.
Hello World
var str: String = “SWIFT”
println(“Hello (str) World”)
Type Inference
var list: String = “Hello World”
var list = “Hello World”
var obj: MyClass = MyClass()
var obj = MyClass()
noun: Inference

verb: Infer
reference report
?
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()
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
!
var value: String?
How to get a content of the Optional value?
var ret = value! // get
var ret = value ?? “It is nil” // getOrElse
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;
}

SWIFT1 Optional

  • 1.
  • 2.
    Java < Scala< Swift Java + Eclipse = COBOL Scala + Idea = cool but tricky Swift + Xcode = easy and fun
  • 3.
    Let’s study Swift. Swift Wer gonna study Swift with Playground.
  • 4.
    Hello World var str:String = “SWIFT” println(“Hello (str) World”)
  • 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.
    ? var value: Optional<Int> varvalue: 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.
    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.
    ! var value: String? Howto get a content of the Optional value? var ret = value! // get var ret = value ?? “It is nil” // getOrElse
  • 9.
    Exceptions Suck We arereally 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; }