Re: forwardInvocation: and keeping compiler happy
Re: forwardInvocation: and keeping compiler happy
- Subject: Re: forwardInvocation: and keeping compiler happy
- From: Nir Soffer <email@hidden>
- Date: Fri, 12 Jan 2007 22:32:47 +0200
On Jan 12, 2007, at 20:42, Gerd Knops wrote:
I have two classes, say ClassA and ClassB. Several instances of
ClassB need to share a single instance of ClassA.
Further I want to make the ClassB objects transparently respond to
everything ClassA implements.
I can't make ClassB a category, as it has it's own instance
variables. And I can't make it a subclass, as I need multiple
ClassB instances share a single ClassA instance.
ClassB can own a shared instance of ClassA, and be a sub class of
ClassA. The big question is "does ClassB is a ClassA"?
The answer is probably no, because class B will have a -
sharedInstance methods, which does not make sense for class A, and
probably instance variables that does not make sense for class B,
because B does not need to state computed by A, because it uses A one.
So I use forwardInvocation: and friends for this, which works fine.
The question is how do I keep the compiler and myself happy. I'd
like the compiler to know about the methods of ClassA in the
context of ClassB, so it can perform all it's type checking. I can
do that by declaring everything ClassA implements as a protocol and
have ClassB tagged as implementing that protocol. Now the compiler
compiles fine and also performs all the type checking. But of
course it warns that ClassB does not actually implement the ClassA
methods.
Formal protocol is not good for this situation, because obviously
ClassB will never implement any of ClassA methods if you want ClassB
to "transparently respond to everything ClassA implements".
Is there a way to get around this, preferably without globally
disabling the otherwise useful warning "method definition for 'x'
not found"?
This will be little better:
@interface A : NSObject
{
// Stuff that needed by all sub classes
}
- doSomthing;
@end
@implementation A
- doSomthing;
{
// actually do something
}
@end
@interface SharedA : A
{
// Stuff that makes sense for the shared instance
}
+ (SharedA *)sharedInstance;
@end
@interface B : A
{
SharedA *implementor;
}
@end
@implementation B
// Use forward invocation and friends to delegate stuff to implementor.
@end
Now you have static type checking for B, because B is an A, and
automatic delegation to SharedA.
Best Regards,
Nir Soffer
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden