Re: New Obj-C root class and forwarding
Re: New Obj-C root class and forwarding
- Subject: Re: New Obj-C root class and forwarding
- From: Marco Scheurer <email@hidden>
- Date: Tue, 18 Mar 2003 18:32:40 +0100
On Tuesday, March 18, 2003, at 08:27 AM, Christopher Sean Morrison
wrote:
[...]
My goal was to create a new root class (i.e. that does not inherit from
NSObject (or Object for that matter)) that conforms loosely to some
interface that defines minimal abstract functionality and data. The
problem I get is that I get a run-time error regarding message
forwarding:
Conforms loosely???
objc: Shape: Does not recognize selector forward::
Abort trap
I believe I roughly understand the parent forwarding mechanism and the
error given would imply that that is one of potentially a handful of
class
mechanisms that I'd need to implement to generate a new root class. Is
this true? At a minimum, this would imply that there are objective-C
run-time semantics that are being assumed by linking with the -lobj
library.
So, here's some code. Please remember that this is all just academic.
that said, we start with a root interface:
@interface Shape {
int x;
int y;
}
- (int)x;
- (int)y;
- (int)x: (int)aNumber;
- (int)y: (int)aNumber;
@end
And then we create a new root class that conforms to the Shape
interface:
#import "Shape.h"
@interface Circle:Shape {
int radius;
}
+ (Circle *)x: (int)xValue y: (int)yValue radius: (int)aRadius;
- (int)radius;
- (int)radius: (int)aNumber;
@end
Your code is not what you're describing. In Objective C the closest
thing to a Java interface is called a protocol, and has no instance
variable. What you call an interface above, Shape, is actually a class
declaration, in need of a definition (@implementation). Circle is not a
root class but the declaration of a subclass of Shape.
The Circle instantiator is presently as follows:
@implementation Circle
+ (Circle *)x: (int)xValue y: (int)yValue radius: (int)aRadius {
return self ;
}
.
.
.
@end
This is also wrong: your "instantiator", a class method returning self
will return the Circle class object, not an instance of Circle
(although this has nothing to do with your problem).
Finally, the driver test code:
#import "Circle.h"
main() {
int i;
Circle *shape[100];
for (i=0; i<100; i++) {
shape[i] = [Circle x: rand() y: rand() radius: rand()];
}
}
At a naive attempt to quell the error, I add a "forward" class method
to
Circle that simply returns self. This quells the error about Circle
not
responding to forward, and results in the error shown about about Shape
not responding to forward. If I add the same class method to Shape,
the
error persists. If I subclass Shape off of NSObject, all is quelled.
1) Can anyone explain what is going on?
2) Can anyone explain how to get what I'm attempting to do to work
without
subclassing off of a root class/interface?
This has already been adressed in other messages: implement the
NSObject protocol. In addition, you should probably give another look
to the available Objective C documentation. It's in
/Developer/Documentation/Cocoa/ObjectiveC .
Marco Scheurer
Sen:te, Lausanne, Switzerland
http://www.sente.ch
_______________________________________________
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.