如何正确加载我的课程?

发布于 2024-12-05 22:13:39 字数 1625 浏览 2 评论 0原文

我在外部文件对中定义了一个类,我们将它们命名为 engine.hengine.m 。另外,它还给了我文件“engineListener.h”。 这是它们的样子:

文件engine.h:

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

文件engine.m

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

文件engineListener.h

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

现在,在我的myController.h中 我有这个:

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

- (IBAction)doSomething:(id)sender;

myController.m 中,这就是我所拥有的:

-(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];
}

现在的情况是:代码编译正确,但“[PhysicsEngine MyOperation:Something]”什么也没做。我确信我错误地实例化了我的课程。我必须加载的“engine.h、engine.m和enginelistener.h”中定义的NSObject不是我制作的,我也没有办法修改它。

我尝试过根据我在互联网上看到的内容做一些虚拟/随机的事情,但 100% 不知道我在做什么。我对 ObjectiveC 或 C/C++ 还不太熟悉,所以请对我温柔一些。我对这个话题完全是菜鸟。 我正在使用 Xcode 4,并且还可以访问 XCode 3.2.6

我应该如何正确加载我的类? 欢迎任何建议。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-12-12 22:13:39

您的类的 -init 应该如下所示:

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

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

    return self;
}

这遵循初始化类的标准设计模式。实际上,您通过在 super 上调用 -init 来进行初始化。之后,如果 self 已正确初始化(几乎总是如此),您将创建 PhysicsEngine 对象。请注意,您的类需要符合 PhysicsEngineListener 协议并实现 -someVariable:

// Declares protocol conformance

@interface MyController : NSObject <PhysicsEngineListener>

// In MyController.m

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

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
}
梦在深巷 2024-12-12 22:13:39

你的界面应该有:

PhysicsEngine *coreEngine;

和 MyController.m init:

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

如果你的代码能够编译,我会感到惊讶。

另外,惯例是类是大写的,而变量不是大写的。可能还有更多的事情要评论,但你应该从那开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文