-2

我已经开始在 cocos2d-iphone 中实现一个游戏;我在我的项目中使用 ARC。

@interface在my 中声明的类的主体中Levelfinder.h,我声明了以下属性:

@property NSMutableArray* pineapples;

在我的主课中,我正在使用这个属性:

for (PineappleModel* pineapple in levelFileHandler.pineapples) 
{
    b2Body* body = [self createPineappleAt:
            [CoordinateHelper levelPositionToScreenPosition:pineapple.position]];
    body->SetLinearDamping(pineapple.damping);
    [pineapplesDict setObject:[NSValue valueWithPointer:body] 
                       forKey:[NSNumber numberWithInt: pineapple.id]];
}

但是当我运行我的代码时,我看到了以下运行时错误:

SIGABRT '-[LevelFileHandler setPineapples:]: unrecognized selector sent to instance 0x9431c60

我是 cocos2d 的新手,但是研究这个问题很久了,我被卡住了。任何形式的帮助将不胜感激。


这是LevelFileHandler.h

@interface LevelFileHandler : NSObject

@property NSMutableArray* pineapples;

@end

现在在我的LevelFileHandler.m,我已经像这样使用了我的菠萝财产

self.pineapples = [NSMutableArray arrayWithCapacity:5];
[self.pineapples addObject:pineappleModel]

很明显,这段代码的目的只是创建一个容量为 5 的数组。

4

2 回答 2

1

那么问题正是所说的。您尝试在未实现 setPineapples 的类对象上调用方法。似乎您尝试在 LevelFileHandler 类型的对象上调用 setPineapples,即使是类名,也不表明它与菠萝有关:P。

您没有在此代码段中的任何位置调用此方法。所以很难判断到底哪里错了。

小心意外的任务。UIKit 的示例是

UITextView text = [UITextView alloc] init];
//few lines later, you do this
text = [SomeClass returnStringAsIDType];
//crash sometime later as textView becomes a string.

由于 Objective-C 是动态类型的,任何将 id 类型的东西分配给具有错误类类型的变量(如果您期望的类型不是实际分配的类型)的任何分配,甚至都不会显示警告。例如,你可能会花很多时间摸不着头脑,想知道为什么你的 UITextView 突然变成了 NSString。

于 2013-05-23T13:15:51.883 回答
0

看起来有类似[[self class] setPineapples:something]in的东西createPineappleAt:

或者,原子引用可能存在问题。默认情况下,属性是原子的和合成的。你能试试:

@interface LevelFileHandler : NSObject

@property (strong, nonatomic) NSMutableArray* pineapples;

@end
于 2013-05-25T15:51:05.157 回答