msk
msk

Reputation: 527

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.

Upvotes: 9

Views: 14863

Answers (2)

Marc W
Marc W

Reputation: 19241

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
   }

Upvotes: 24

cobbal
cobbal

Reputation: 70743

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

if (!date) {
    ...
}

or more explicitly

if (date == nil) {
    ...
}

Upvotes: 8

Related Questions