开发者

How to load correctly my class?

I have a class defined in a an external pair of files, let's name them engine.h and engine.m . Also, it was given to me the file "engineListener.h". this is how they look:

File engine.h:

@interface coreEngine: NSObject {
   NSString *displaydValue;
   id <coreEngineListener> listener;
}
-(coreEngine*)initWithListener:(id <coreEngineListener>) _listener;
//...

File engine.m

//imports here
@interface coreEngine()
-(Boolean) MyOperation: ... ... (etc)
@end

File engineListener.h

//imports here...
@class coreEngine;
@protocol coreEngineListener <NSObject>
@required
-(void) someVariable:(coreEngine *)source;
@end

Now, in my myController.h I have this:

//... imports and include ...
@interface myController : NSObject
{
    coreEngine *PhysicsEngine;
}

- (IBAction)doSomething:(id)sender;

And in the myController.m this is what I have:

-(id) init
{
    NSAutorelease *pool = [[NSAutoreleasePool alloc] init];
    coreEngine *PhysicsEngine = [[coreEngine alloc] init];

    [PhysicsEngine release];
    [pool drain];
    return self;
}

- (IBAction)doSomething:(id)sender
{
    [PhysicsEngine MyOperation:Something];
}

The thing now is: the code compiles correctly, but the "[PhysicsEngine MyOperation:Something]" is doing nothing. I'm sure I'm instantiating my class wrongly. The NSObject defined in "engine.h engine.m and enginelistener开发者_运维技巧.h" that I have to load was not made by me, and I have no means to modify it.

I've tried doing some dummy/random things based on what I've seen around on the internet, without knowing 100% what I was doing. I'm not even close to be familiar with ObjectiveC or C/C++, so be gentle with me. I'm totally noob on this subject. I'm using Xcode 4, and I have also access to XCode 3.2.6

How should I load my class properly? Any advice is welcome. Thanks


Your class's -init should look something like this:

- (id)init
{
    self = [super init];

    if (self != nil)
    {
        coreEngine = [[PhysicsEngine alloc] initWithListener:self];    // I assume you don't have a property declared
    }

    return self;
}

This follows a standard design pattern for initializing classes. You actually do the initialization by calling -init on super. Afterwards, if self was initialized properly (as it almost always will be), you create your PhysicsEngine object. Note that your class needs to conform to the PhysicsEngineListener protocol and implement -someVariable:.

// Declares protocol conformance

@interface MyController : NSObject <PhysicsEngineListener>

// In MyController.m

- (void)someVariable:(PhysicsEngine *)source
{
    // Do whatever this is supposed to do
}


Your interface should have:

PhysicsEngine *coreEngine;

and MyController.m init:

PhysicsEngine *coreEngine = [[PhysicsEngine allow] init];

I would be surprised if your code would compile at all.

Also, the convention is that classes are capitalised, and variables are not. There are probably more things to comment on, but you should start with that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜