Wednesday, July 15, 2015

Checking for nil

This occurred to me for a week.
In Objective-C, to check for nil, we can just
if (varName) {
    //not nil
} else {
    //nil
}
and do note that one handy thing in objective-c is that when you declare a variable, it assigns 0.

But thanks to optionals in swift, variables are not supposed to be nil.
So I was checking if cookies are persisting. And found out that I always return true, even if the println(cookie) result is
[]


So in Swift, you have to either call isEmpty
if cookieArray?.isEmpty == false{
     //cookie stored
} else {
    //no cookie :(
}

No comments:

Post a Comment