Re: Re: Selector Not Recognized
Re: Re: Selector Not Recognized
- Subject: Re: Re: Selector Not Recognized
- From: "Shawn Erickson" <email@hidden>
- Date: Tue, 11 Jul 2006 15:41:13 -0700
On 7/11/06, David Alter <email@hidden> wrote:
Sorry about that. That was not the example I intended to send. Lets
see if this does a better job of explaining this.
//The interface for the parent class
@interface MyParent : NSObject {
int somedata
}
@end
//The interface for the sub class
@interface MySub : MyParent {
}
- (int) getSomeDataPlusOne; // This will access somedata from the
parent class add one to it and return it.
@end
//Some code some place
//Make an instance on MyParent
MyParent * parent = [[MyParent alloc] init];
//Cast MyParent into a pointer of MySub.
MySub * sub = (MySub *) parent;
//Access a method of MySub.
int val = [sub getSomeDataPlusOne]; // It chokes here with "selector
not recognized"
The key here is the pointer is a pointer to MyParent, NOT MySub.
Sense MySub has not instance variables can I cast it into MyParent.
If not what is the suggested way of doing things.
Well if parent is a pointer to an instance of MyParent (as you code
outlines) then you are trying to send the "getSomeDataPlusOne" message
to an instance of MyParent. If instances of MyParent don't respond to
that message then you get what you are reporting.
Casting your "parent" pointer to a pointer of MySub type doesn't
change the kind of the you have object in anyway. The object itself
knows of what class it is and what it can respond to.
This also seems like a dangerous thing to attempt in C++ (if even
possible...? some kind of function dynamic cast?).
Anyway consider adding getSomeDataPlusOne as a category to MyParent,
among a few other options that come to mind (not knowing the bigger
picture).
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden