페이지

2016년 11월 20일 일요일

Swift tuples, optional value, optional binding, throws, catch, comparisoin, assert

//1. tuples

let httpResponse = ( 404, "File Not Found" )
let (statusCode, errorMsg) = httpResponse
let ( sCode, _ ) = httpResponse
let (code,msg) = (state:505, message:"hi")


//2. optional value

let num = "123"
let numInt = Int(num)
print(numInt)


//3. optional binding

if let a = Int("4"), let b = Int("43") , a < b && b < 100 {
    print("4<43<100 \(a),\(b)")
}
else {
    print("nil")
}

//4. throw error

func canThrowAnError() throws {
    //this function may or not throw an error  
}

do {
    try canThrowAnError()
} catch {    

}

//5. comparison

(1, "zebra") < (2, "apple")   // true because 1 is less than 2; "zebra" and "apple" are not compared
(3, "apple") < (3, "bird")    // true because 3 is equal to 3, and "apple" is less than "bird"
(4, "dog") == (4, "dog")      // true because 4 is equal to 4, and "dog" is equal to "dog”


//6. assert

let aa = -1
assert(aa > 0 ,"number should be larger than zero" )


댓글 없음:

댓글 쓰기