Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if NSDate holds a date or is empty

What would be the best way to see if an NSDate is actually set to a date or is empty? Returning its description or changing it to string returns "(null)"...?

Thanks.

like image 896
msk Avatar asked Mar 08 '10 18:03

msk


2 Answers

If you want to see if an instance of NSDate (or any object) is nil, just compare it to nil. A non-null NSDate object will never be empty; it always contains a date. Both -init and +date return an NSDate object initialized to the current date and time, and there is no other way to create an instance of this class.

   if(someData == nil) {
      // do stuff
   }
like image 60
Marc W Avatar answered Oct 17 '22 16:10

Marc W


if it doesn't hold a date, then you have a nil pointer, so simply

if (!date) {
    ...
}

or more explicitly

if (date == nil) {
    ...
}
like image 34
cobbal Avatar answered Oct 17 '22 15:10

cobbal