Matrosov Alexander Matrosov Alexander - 1 year ago 86
Objective-C Question

NSDateFormatter return (null) string when NSDate is nil

I use date formatter to get new string, but in some cases startDate can be nil.

resultDate = [NSString stringWithFormat:@"%@", [formatter stringFromDate:startDate]];

And then resultDate get
string (NSTaggedPointerString), but I expect to get just nil in case if formatter get nil as a parameter of date.

dan dan
Answer Source

The currently accepted answer isn't actually right.

stringFromDate does return nil when you pass it a nil date.

The reason your string is "(null)" is because [NSString stringWithFormat:"%@", nil] returns the string "(null)"