调用外部类方法导致崩溃 Objective-c

发布于 2024-10-27 04:18:43 字数 1289 浏览 6 评论 0原文

我有两个类,使用 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 技术交流群。

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

发布评论

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

评论(2

兔小萌 2024-11-03 04:18:44

您可能最终不会创建 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).

彡翼 2024-11-03 04:18:44

好的,我发现问题了,当我调用
时,正在执行无限循环
[一级映射]。我向 Level1 方法映射添加了一条 NSLog 语句,以查看控制台是否填充了 NSLog。

仅当我尝试初始化 Construct *constr 对象时才会创建此无限循环...

Construct.mm

-(id) init
{
    if( (self=[super init])) {
        //ALLOCS
        _Eggs    = [[NSMutableArray alloc] init];
        _Block1s = [[NSMutableArray alloc] init];
        _Block2s = [[NSMutableArray alloc] init];

        //Begin Mapping of environment
        Level1 *level1 = [[[Level1 alloc] init] autorelease];
        [level1 mapping];
         }
   return self;
}

Level1.mm

@implementation Level1

-(void) mapping{

    NSLog (@"Mapping");
    Construct *constr = [[Construct alloc]init];
    return;
}

@end  

控制台:

2011-03-28 09:43:24.980 Construct[14661:207] Mapping  
2011-03-28 09:43:24.981 Construct[14661:207] Mapping  
2011-03-28 09:43:24.982 Construct[14661:207] Mapping  
2011-03-28 09:43:24.982 Construct[14661:207] Mapping  
2011-03-28 09:43:24.983 Construct[14661:207] Mapping  
2011-03-28 09:43:24.983 Construct[14661:207] Mapping  
2011-03-28 09:43:24.984 Construct[14661:207] Mapping  
2011-03-28 09:43:24.985 Construct[14661:207] Mapping  
2011-03-28 09:43:24.985 Construct[14661:207] Mapping  
2011-03-28 09:43:24.986 Construct[14661:207] Mapping  
2011-03-28 09:43:24.986 Construct[14661:207] Mapping  
...

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

-(id) init
{
    if( (self=[super init])) {
        //ALLOCS
        _Eggs    = [[NSMutableArray alloc] init];
        _Block1s = [[NSMutableArray alloc] init];
        _Block2s = [[NSMutableArray alloc] init];

        //Begin Mapping of environment
        Level1 *level1 = [[[Level1 alloc] init] autorelease];
        [level1 mapping];
         }
   return self;
}

Level1.mm

@implementation Level1

-(void) mapping{

    NSLog (@"Mapping");
    Construct *constr = [[Construct alloc]init];
    return;
}

@end  

Console:

2011-03-28 09:43:24.980 Construct[14661:207] Mapping  
2011-03-28 09:43:24.981 Construct[14661:207] Mapping  
2011-03-28 09:43:24.982 Construct[14661:207] Mapping  
2011-03-28 09:43:24.982 Construct[14661:207] Mapping  
2011-03-28 09:43:24.983 Construct[14661:207] Mapping  
2011-03-28 09:43:24.983 Construct[14661:207] Mapping  
2011-03-28 09:43:24.984 Construct[14661:207] Mapping  
2011-03-28 09:43:24.985 Construct[14661:207] Mapping  
2011-03-28 09:43:24.985 Construct[14661:207] Mapping  
2011-03-28 09:43:24.986 Construct[14661:207] Mapping  
2011-03-28 09:43:24.986 Construct[14661:207] Mapping  
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文