如何正确加载我的课程?
我在外部文件对中定义了一个类,我们将它们命名为 engine.h 和 engine.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的类的
-init
应该如下所示:这遵循初始化类的标准设计模式。实际上,您通过在
super
上调用-init
来进行初始化。之后,如果self
已正确初始化(几乎总是如此),您将创建PhysicsEngine
对象。请注意,您的类需要符合PhysicsEngineListener
协议并实现-someVariable:
。Your class's
-init
should look something like this:This follows a standard design pattern for initializing classes. You actually do the initialization by calling
-init
onsuper
. Afterwards, ifself
was initialized properly (as it almost always will be), you create yourPhysicsEngine
object. Note that your class needs to conform to thePhysicsEngineListener
protocol and implement-someVariable:
.你的界面应该有:
和 MyController.m init:
如果你的代码能够编译,我会感到惊讶。
另外,惯例是类是大写的,而变量不是大写的。可能还有更多的事情要评论,但你应该从那开始。
Your interface should have:
and MyController.m 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.