我有一个NSMutableArray被定义为一个属性,合成并且我已经分配了一个新创建的NSMutableArray实例。但是在此之后,当我尝试向NSMutableArray添加一个对象时,我的应用程序总是崩溃。NSMutableArray在正确初始化后添加时崩溃
Page.h
@interface Page : NSObject
{
NSString *name;
UIImage *image;
NSMutableArray *questions;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, copy) NSMutableArray *questions;
@end
Page.m
@implementation Page
@synthesize name, image, questions;
@end
相关代码
Page *testPage = [[Page alloc] init];
testPage.image = [UIImage imageNamed:@"Cooperatief leren Veenman-11.jpg"];
testPage.name = [NSString stringWithString:@"Cooperatief leren Veenman-11.jpg"];
testPage.questions = [[NSMutableArray alloc] init];
[testPage.questions addObject:[NSNumber numberWithFloat:arc4random()]];
调试器显示,目前我使用testPage.questions = [[NSMutableArray alloc] init];
testPage.questions的类型从NSMutableArray *更改为__NSArrayL *(或__NSArrayI *,不确定)。我怀疑这是问题,但我觉得这很奇怪。任何人都知道这里发生了什么?
嗯,没想到会这样,但它的工作原理!我使用了'copy',因为在这里有很多问题建议在任何可变的东西时使用复制。谢谢:D – SpacyRicochet 2011-02-23 22:57:13
@SpacyRicochet:当你想确保得到的属性值是*不可变*时,你可以使用'copy'。你最常使用'NSString'属性来看这个:你'复制'它们以使得字符串本身不能从你下面改变。由于你的财产*被认为是可变的,所以你不能使用'copy'。 :) – 2011-02-23 23:53:28
谢谢!这将花费我更多的时间来弄清楚。 :) – 2011-04-12 15:21:36