Re: Help on subclassing
Re: Help on subclassing
- Subject: Re: Help on subclassing
- From: Joachim <email@hidden>
- Date: Thu, 1 Dec 2005 11:01:22 -0800
Hi Jon,
Thanks for the input. When reading "The Objective-C Programming
Language" a few months back, I didn't think I would need protocols...
But still, it doesn't seem to do the trick.
It still requires class A to implement the someMethod, and that's
exactly what I don't want. That method will only exist in A's
subclasses, B and C. So I might want to make a protocol that both B
and C conforms to, but it still doesn't solve my problem:
I want to call [self someMethod] in my A class code, knowing that all
A class code, including someMethod, will only be executed within a B
or C object, so that someMethod only needs to be present in the B and
C subclasses.
If I'm doing what you suggest below, I get a compile error, saying
that A doesn't fully implement the 'myProtocol' protocol, because
someMethod is not present in A.
Other ideas will be much appreciated.
Thank you,
Joachim
On 30-Nov-05, at 4:13 PM, Jon Adelman wrote:
Declare a protocol for the methods that you are going to be using
in both B and C
@protocol myProtocol
- (NSRect) someMethod;
@end
Make A Subscribe to that protocol
@interface A : NSObject <myProtocol>
@end
On 30-Nov-05, at 4:09 PM, Joachim wrote:
Esteemed experts,
I'm working with 2 classes, B and C, that are quite similar to
each other. One of them is a preview of the other. They both
inherit from their superclass, A, that will never be instantiated
(like an abstract class). All functionality that is the same for
both B and C is of course placed in A, and the methods that differ
are put in the respective B and C subclasses.
From within the generic code (in the A class), I'd like to call
code that's declared and will be run in the respective B and C
subclasses. That is, from within A I'd like to call [self
someMethod], where someMethod only exists in B and C - not in A.
How do I do this?
In my attempts, the compiler warns that A may not respond to
someMethod which is fine, because I know it will at runtime. And
it works as expected at runtime. But if someMethod returns a
value, the compiler throws an error:
NSRect myRect = [self someMethod];
-> error: invalid initializer
(or
NSRect myRect;
myRect = [self someMethod];
-> error: incompatible types in assignment)
My workaround is to declare the method in A where I just return a
"dummy" value, because it's never used anyway. But it's not very
nice to look at and shouldn't be necessary. Any solution to this?
Thanks you for your help,
Joachim
_______________________________________________
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