views:

1727

answers:

1

I'm learning objective-c, working through Aaron Hillegass' book "Cocoa Programming for Mac OS X - Third Edition". I'm working in Xcode to develop some of the example code, and keep getting a compiler warning on the last line of this method:

- (void) setEntryDate:(NSCalendarDate *) date {
    [date retain];
    [entryDate release];
    entryDate = date;
}

The warning reads "warning: assignment from distinct objective-c type". What causes this warning? How do I prevent it from happening again?

It doesn't seem to affect execution of the program, but I don't understand what the problem is, or even if there really is a problem (could this just be a paranoid compiler?).

+2  A: 

NSCalendar and NSCalendarDate are indeed distinct types. You should decide which one you want entryDate to be (probably NSCalendarDate, judging by the "date" thing").

Chuck
Got it. Why is the compiler warning me about this? Does it just have moral objections to polymorphism?
Brian
That's the point of static type declarations — to say, "I expect this type here, so warn me if I have something else."
Chuck
A better question is why you'd want to have an NSCalendar and NSCalendarDate in the same role. They have different purposes and different methods. This seems almost certainly like an error, which is why compile-time type checking can be useful. Were you doing it for a reason?
Chuck