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?
It’s not necessary. The cast is simply for clarity, as the property is already defined as a
NSDate, here: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:
However, in ARC, you would use a
__bridgecast instead.