objective-c中的箭头操作符 [英] arrow operator in objective-c
问题描述
我有一个问题,这是代码:
I have a question, here's the code:
@interface MyFoo : NSObject {
NSString *nameStr;
}
@end
@implementation MyFoo
- (id)init {
self = [super init];
if (self) {
self->nameStr = [@"some value of the string that is set right into the private ivar" copy];
}
return self;
}
@end
问题是:忽略所有C ++规则,忽略内存转储漏洞,为什么我不应该使用这样的箭头操作符语法?
在Apple文档中是否存在一个规则,说明它是不正确的,因为在将来的类中可能表示与运行时等结构的指针不同?
The question is: ignoring all the C++ rules, ignoring memory dump vulnerability, why exactly I shouldn't use such arrow operator syntax? Is there somewhere in Apple documentation a rule which says that it's incorrect because in future class may be represented differently than a pointer to a struct in runtime etc. ?
提前致谢!
推荐答案
使用 self-> someIvar
与 someIvar
相同。这没有错,但也不需要。
The use of self->someIvar
is identical to someIvar
. It's not wrong but it's not needed either.
我使用箭头符号的唯一一次是在 copyWithZone的实现中:
所以我可以复制每个没有属性的ivars。
The only time I use the arrow notation is in an implementation of copyWithZone:
so I can copy each of the ivars that don't have properties.
SomeClass *someCopy = ...
someCopy->ivar1 = ivar1; // = self->ivar1
someCopy->ivar2 = ivar2; // = self->ivar2
你在哪里看到任何说你不应该使用的东西箭头运算符语法?
Where are you seeing anything that says you shouldn't use such arrow operator syntax?
这篇关于objective-c中的箭头操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!