Thursday 15 August 2013

iphone - iOS, using underscore vs using iVar directly -


It has been said a lot, but the question is when will you use each of these methods. Please use other than Setter and Gator endless loop

example

.h -
@property (nanatomic, strong) NSMutableArray * mutArray .m -
@synthesize mutArray = _mutArray;

1) Do I want to:
_mutArray = [[NSMUTABELARROL] IITIT]; or OR << CODE> Self.mutArray = [[NSMutableArray alloc] init];
Why would I do each of them, and what's the difference?

2) If I want to add an object to it ...
[_ mutArray addObject: object]; or OK [self.mutArray addobject: object];

And why ?! Thank you very much!

You can only type your code with init and Dealloc or the implementation details (such as inside the accessor, or exactly where you actually need a memory address). In addition to those places, you should always use the accelerator, which means instead of [foo] is _foo .

self.foo Actually, the actual call is about syntactic sugar, which is [self foo] It is important to understand that Self.foo is a standard ObjC messaging, and this means that the same thing as [self foo] . From the conference, you should only use dot-syntax when referring properties.

The direct use of the pre-ARC, the iver was the # 1 cause of crashing in my experience. When specifying an Ever without ARC, you are less likely to reach 100% on the scope of the program.

Since ARC, I still argue that you should always use the accessories (with the above exceptions)), but the reasons are more subtle, the main reason is that an accelerator is customized Can be either in the existing class, sub-class, or through KVO (which is outside of your code). If you reach straight ivar, you will bypass it. For example, it is to say that the property is weak (which is very common). Then if you use EVER before making it, you will get a micro bug. So you should remember, for that property, always use the accelerator. Similarly, you can call setNeedsDisplay or post a notification or like < P> If you have a simple rule that says, "I always use the accessories" then the code is easy to see and it's right. In some cases you need to circumvent the accessor, _ says "Hey, here's the note, I'm doing something weird."

If you have the rule "I use users for those properties that need it, but not for those people" who do not see the code and it's right that it's right. Did the previous developer use Ivar because it was needed or just because he felt it? Can you change it or not? It is very hard to know

Using archives, even after ARC there is also good defensive programming and I highly recommend it.

No comments:

Post a Comment