Reputation: 947
I've been working through Apple's "Your Second IOS App" tutorial" and I've noticed that every time I use an NSDateFormatter
for producing a date string I am expected to cast the input date object.
e.g. (Page - under "To implement the configureView method")
BirdSighting *theSighting = self.sighting;
if (theSighting) {
self.birdNameLabel.text = [theSighting name];
self.locationLabel.text = [theSighting location];
self.dateLabel.text = [formatter stringFromDate:(NSDate *)theSighting.date]; // Here
}
However I know that in the BirdSighting
class that the date
property is always an NSDate
object.
So I was wondering why the tutorial always casts the input, is there some sort of objective-c convention or framework convetion that recommends that you do this? and if so why?
Upvotes: 4
Views: 118
Reputation: 119292
The cast is unnecessary and should be removed in my opinion.
The property is already defined as an NSDate
on the data model object (unless there is a later step that redefines it to id
or something, which is doubtful) so it is giving no extra information to readers of the code or the compiler.
Upvotes: 2
Reputation: 55583
It's not necessary. The cast is simply for clarity, as the property is already defined as a NSDate
, here:
@property (nonatomic, strong) NSDate *date;
The only situation where a cast would be necessary in that form would either be in non-arc when dealing with core foundation, like this:
CFDateRef asCFDate;
NSDate *asNSDate = (NSDate *) asCFDate;
However, in ARC, you would use a __bridge
cast instead.
Upvotes: 4