为什么我不应该在init方法中使用访问器方法 [英] Why shoudn't I use accessor methods in init methods
本文介绍了为什么我不应该在init方法中使用访问器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
摘自Apple文档中有关内存管理:
From Apple documentation about Memory Management :
您不应该使用访问器方法设置实例变量的唯一地方是init方法和dealloc.要使用一个数字对象代表零来初始化一个计数器对象,可以实现如下的init方法:
The only places you shouldn’t use accessor methods to set an instance variable are in init methods and dealloc. To initialize a counter object with a number object representing zero, you might implement an init method as follows:
要允许使用非零计数初始化计数器,您可以实现initWithCount:方法,如下所示:
To allow a counter to be initialized with a count other than zero, you might implement an initWithCount: method as follows:
- initWithCount:(NSNumber *)startingCount {
self = [super init];
if (self) {
count = [startingCount copy];
}
return self;
}
为什么不呢?
推荐答案
查看全文