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>
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. 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?
How to get a content of the Optional value?
var ret = value! // get
var ret = value ?? “It is nil” // getOrElse
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;
}