Self 带有数组和 addObject
当将对象插入具有属性的数组时,是否有任何理由使用 self 调用 getter/setter?即
[self.myArray insertObject: myObject];
或者我可以只使用:
[myArray insertObject: myObject];
要点是:
.h
@interface ArrayViewController : UIViewController <UITextFieldDelegate>
{
NSMutableArray *myArray;
int itemNumber;
}
@property (nonatomic, retain) NSMutableArray *myArray;
@end
.m
- (IBAction)createMyArray
{
self.myArray = [[NSMutableArray alloc] initWithObjects: nil];
}
-(IBAction) addItemToMyArray
{
NSString *myString = [[NSString alloc] initWithFormat:@"item %d",itemNumber];
[myArray addObject: myString];
//[self.myArray addObject: myString]; //Or should I use self?
[myString release];
NSLog(@"myArray = %@", myArray);
itemNumber++;
}
//- (void)dealloc etc. not shown
When inserting an object into an array with a property is there any reason to invoke the getter/setter with self? i.e.
[self.myArray insertObject: myObject];
Or can I just use:
[myArray insertObject: myObject];
the gist would be:
.h
@interface ArrayViewController : UIViewController <UITextFieldDelegate>
{
NSMutableArray *myArray;
int itemNumber;
}
@property (nonatomic, retain) NSMutableArray *myArray;
@end
.m
- (IBAction)createMyArray
{
self.myArray = [[NSMutableArray alloc] initWithObjects: nil];
}
-(IBAction) addItemToMyArray
{
NSString *myString = [[NSString alloc] initWithFormat:@"item %d",itemNumber];
[myArray addObject: myString];
//[self.myArray addObject: myString]; //Or should I use self?
[myString release];
NSLog(@"myArray = %@", myArray);
itemNumber++;
}
//- (void)dealloc etc. not shown
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从概念上讲,这并不重要,只要您的 getter 方法仅返回现有字段值并且不执行某些“及时”分配或类似操作即可。
然而,最好的做法是制定一项您坚持执行的政策(个人或团体),以便该政策的警告成为您的第二天性。不断地切换样式会导致草率、有错误的代码。
我倾向于总是使用
self.
作为属性,只是为了提醒自己它们实际上是属性,并减少我在不使用属性表示法的情况下意外设置值的可能性。Conceptually, it doesn't matter, so long as your getter method only returns the existing field value and doesn't, eg, do some "just in time" allocation or some such.
However, it's good practice to come up with a policy (personal or group) that you stick with, so that the caveats of that policy become second nature. Constantly switching styles results in sloppy, buggy code.
I tend to always use the
self.
for properties, just to remind myself that they are, in fact, properties, and to make it less likely that I'll accidentally set the value without using the property notation.两者都可以,但你需要意识到你在做什么。使用 self. 将调用 setter/getter 方法,而其他方法将直接访问变量。尽管完全有效,但不鼓励在初始化程序和 dealloc 方法之外直接使用该变量。原因是您正在失去该属性的好处,尤其是使用
self.
进行设置,因为它将正确地为您分配/复制/保留该值。不直接使用属性变量的另一个原因是原子性,但在您的情况下,您将其声明为非原子性。Either will work but you need to be aware of what you are doing. Using
self.
will invoke the setter/getter methods while the other will just access the variable directly. Using the variable directly, while perfectly valid, is discouraged outside of the initializer and dealloc method. The reason is you are losing out on the benefits of the property, especially setting usingself.
because it will properly assign/copy/retain the value for you correctly. Another reason not use property variables directly is because of atomicity but in your case you declared it asnonatomic
.两者都很好。这主要是一种风格选择。使用
self.myArray
将导致调用 getter[self myArray]
。Both of those are fine. It's mostly a stylistic choice. Using
self.myArray
will result in a call to the getter[self myArray]
.使用 alloc/init 时,不应将返回值设置为属性,因为这些值将保留两次:
使用
或
用于初始化。
但插入操作是等效的。
When using alloc/init you should not set the returned value to a property, as these will retain twice:
use
or
for the initialization.
The insert operations are equivalent though.
我通常会跳过 getter,因为我很少发现它有价值,而且它会稍微扰乱代码的可读性。但是,我倾向于使用 setter,因为我发现允许自动生成的 setter 方法处理保留/释放语义更容易
I typically skip the getter because I rarely find it valuable and it clutters up the readability of the code a bit. However, I tend to use the setter because I find it easier to allow the auto-generated setter methods to handle the retain/release semantics
在您的情况下,没有义务使用 self.myArray 但对于这种情况,它将是一种义务:
区分类属性和函数参数。
In your case it's not an obligation to use self.myArray but for this case belloaw it will be an obligation:
to difference between the class attribut and the function argument.