Question about NSCoder and decodeObject
Question about NSCoder and decodeObject
- Subject: Question about NSCoder and decodeObject
- From: Matt Budd <email@hidden>
- Date: Fri, 19 Sep 2003 13:51:45 -0600
Hi,
I just starting to learn Cocoa and Objective-C (using Hillegass' brown
book). I have a question about how stuff works with the NSCoder and its
decodeObject method (page 124 for those who have the book).
In it, he defines a class called Person as (partially):
@interface Person : NSObject <NSCoding> {
NSString *sPersonName;
}
//Accessor methods
- (NSString *)sPersonName;
- (void)setSPersonName: (NSString *)psName;
@end
Then in the implementation of initWithCoder for the Person class:
- (id)initWithCoder: (NSCoder *)poCoder
{
if (self = [super init]) {
[self setSPersonName: [poCoder decodeObject]];
}
return self;
}
Now my question is why the middle line of the initWithCoder method
doesn't create a compile error or even a warning. According to the
documentation, the decodeObject method of the NSCoder returns an "id"
(which is a "NSObject *", right?). If this is the case, then aren't we
assigning a "NSString *" parameter (psName in the accessor method) a
NSObject * variable? This would be okay if it were the other way
around, but you can't treat a superclass as a subclass, right (NSString
is a subclass (eventually) of NSObject)?
To me it's like saying that this function requires a banana, but we are
passing it a fruit object (that we know behind the scenes is a banana).
It will work in the end, but the shouldn't the compiler at least warn
us that we might be sending it an apple?
Thanks for any info...
- Matt
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.