سؤال

I was reading this official guide: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW13 and I'm not sure if it's referring to old ways of dealing with reference counting or is just for demonstration how it works - but should one use retain/release manually like in example with accessors?

هل كانت مفيدة؟

المحلول

You are not allowed to use retain with ARC. However it still works the same in the background as without ARC (and as described in that documentation you linked), but the retain and release calls are added by the compiler as necessary. You don't have to deal with that.

This setter:

- (void)setCount:(NSNumber *)newCount {
    [newCount retain];
    [_count release];
    // Make the new assignment.
    _count = newCount;
}

should look like this, when using ARC:

- (void)setCount:(NSNumber *)newCount {
    // Make the new assignment.
    _count = newCount;
}

نصائح أخرى

you not use retain with ARC but It is possible to disable ARC for individual files by adding the -fno-objc-arc compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top