NSArray
和NSMutableArray
发送-retain
对象加入时,他们,所以适当的事情做的是-release
你添加的对象明确,或使用自动释放的对象。
这里是为Tuner
类的-init
方法,一个简单的例子:
- (id)init
{
NSInteger i;
Note *note;
self = [ super init ];
if (self) {
notes = [[ NSMutableArray alloc ] initWithCapacity: 88 ];
for (i = 0; i < 88; i++) {
note = [[ Note alloc ] initWithKey: i ];
[ notes addObject: note ];
[ note release ];
}
}
return(self);
}
您需要+alloc
每一个音符对象和-release
它,因为它保留其添加到阵列。为了消灭当你完成所有提示对象,请确保您的Tuner
类释放阵列中的-dealloc
方法:
- (void)dealloc
{
[ notes release ]; // this sends release to all objects in the array, too
[ super dealloc ];
}
谢谢,与NSMutableArray或只是标准的NSArrays我需要在数组的末尾添加零?我在某处读过这些内容,但不确定何时该做这件事。 – hrtachetosing 2011-05-17 15:25:28
'nil'被称为哨点值。它告诉接收者变量长度变量列表的末尾在哪里。你只需要使用特定的'NSArray'(及其子类)和'NSDictionary'方法,如'-initWithObjects:',例如:'array = [[NSArray alloc] initWithObjects:a,b,c,nil];' 。由于'-initWithObjects:'接受任意数量的参数,因此需要通过某种方式告知接收方哪个参数是最后一个参数。 'nil'就是为了这个目的。 – 2011-05-17 15:42:51
只是为了澄清:'-addObject:'不需要定位零值,因为您只需添加一个对象。 – 2011-05-17 15:45:32