views:

173

answers:

2

Hi,

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.

+1  A: 

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

if (!date) {
    ...
}

or more explicitly

if (date == nil) {
    ...
}
cobbal
+4  A: 

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
   }
Marc W
Thanks, I had actually tried this, but don't know, there is something messed up, while the condition works, my later code gets to have some problem then. I am using the check to put different data into a table cells. What works for me is changing the date into a string and checking whether it is a "(null)" or not. Amazingly, that gives the right results. I have no idea how a simple check could affect the later code of constructing the cells. Coz I don't do anything different other than the if condition.
msk
We'd need to see code to help you. What you are saying isn't possible without something else being wrong.
Marc W
Marc, thanks but I actually figured out what the problem was. And yes the problem was with something else, the if condition had less to do with it. :)
msk