调用外部类方法导致崩溃 Objective-c
我有两个类,使用 Box2d、Cocos2d。
Construct.mm 和 Level1.mm
Construct包含了box2d物理引擎中创建不同对象的所有方法
Level1 包含有关绘制对象的信息。
在构造实现中我有:
Construct.mm
-(void) someInitMethod{
Level1 *level1 = [[Level1 alloc] init];
[level1 mapping];
}
-(void) someCreateRectMethod:(argue)ments{
//create rect
}
在 Level1 实现中,我有:
Level1.mm
-(void) mapping{
Construct *constr;
if (constr == nil) constr = [[Construct alloc] init];
[constr someCreateRectMethod:(argue)ments];
}
现在,我知道 Level1 *level1 的分配工作正常。它跳转到 -(void)mapping{ }.
我的问题是创建 Construct *constr 对象并初始化它。没有 if(constr == nil) 语句。模拟器将立即死亡,甚至在发布任何错误报告之前。
如果我放置 if(constr == nil) 然后尝试调用 [constr someCreateRectMethod:arguement];
控制台报告:
* 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因: '-[Level1 someCreateRectMethod:]: 无法识别 选择器发送到实例 0x5557fc0'
我认为这是因为它没有分配,因此尝试发送到自身(Level1)而不是构造。
为什么我的 Construct *constr 类对象没有分配?就像我说的,没有错误报告它发生。我已经导入了 Construct.h 文件。我花了过去 4 个小时试图让它发挥作用。
编辑
我刚刚让 Layer 1 继承自 Construct。而不是调用 [constr someCreateRectMethod];我只是调用 [super someCreateRectMethod];
理论上这应该可行,但是模拟器崩溃并且没有记录错误报告。
再次感谢您的宝贵时间。
奥利弗.
I have two classes, using Box2d,Cocos2d.
Construct.mm and Level1.mm
Construct contains all the methods for creating different objects in the box2d physics engine
Level1 contains the information on the plotting of objects.
In the construct implementation I have:
Construct.mm
-(void) someInitMethod{
Level1 *level1 = [[Level1 alloc] init];
[level1 mapping];
}
-(void) someCreateRectMethod:(argue)ments{
//create rect
}
In the Level1 implementation I have:
Level1.mm
-(void) mapping{
Construct *constr;
if (constr == nil) constr = [[Construct alloc] init];
[constr someCreateRectMethod:(argue)ments];
}
Now, I know the allocation of Level1 *level1 is working fine. It jumps to -(void)mapping{
}.
My problem is in creating the Construct *constr object and initialising it. Without the if(constr == nil) statement. The simulator will die straight away, before even posting any error report.
If i put the if(constr == nil) and then try to call [constr someCreateRectMethod:arguement];
The console reports:
* Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'-[Level1 someCreateRectMethod:]: unrecognized
selector sent to instance 0x5557fc0'
I think this is because its not allocing, therefore trying to send to self (Level1) rather than Construct.
Why isnt my Construct *constr class object not allocing? Like I said, no error reporting it occurring.. I have imported the Construct.h file. I've spent the last 4 hours trying to get this to work.
EDIT
I just made Layer 1 inherit from Construct. And instead of calling [constr someCreateRectMethod]; I simply call [super someCreateRectMethod];
This should theoretically work, but Simulator crashes and no error reporting is logged..
Once again, Thanks for your time.
Oliver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能最终不会创建 constr - 初始值未定义,因此不太可能为零。如果你的代码以其他方式终止于 [[constr alloc] init] ,那么你似乎未能子类化 NSObject (你应该这样做)并且没有实现 init (你经常想要这样做)。
You probably don't end up creating constr - the initial value is undefined and therefore very unlikely to be nil. If your code otherwise terminates at [[constr alloc] init] then it seems likely that you've failed to subclass NSObject (which you should) and not implemented init (which you often want to).
好的,我发现问题了,当我调用
时,正在执行无限循环
[一级映射]。我向 Level1 方法映射添加了一条 NSLog 语句,以查看控制台是否填充了 NSLog。
仅当我尝试初始化 Construct *constr 对象时才会创建此无限循环...
Construct.mm
Level1.mm
控制台:
Okay, I've found the problem, an infinite loop is being executed When I call
[level1 mapping]. I added an NSLog statement to the Level1 method mapping to see the console filling up with NSLogs.
This infinite loop is only created when I try to init the Construct *constr object...
Construct.mm
Level1.mm
Console: