The ARC or not?
Automatic Reference Counting is an important progress since the development of objC
For developers, any feature that makes development easier and simplifies code should be understood and used. We should take advantage of every “lazy” opportunity to decompose and control the complexity of software development in a small range, so that every small task after decomposition can be mastered and maintained by the novice.
In the spirit of simplifying development, ARC is definitely a technological advance that can’t be rejected. ARC came out with iOS5, and now iOS8 is coming out, are you still manually writing retain release? In addition to clinging to the idea, much of the fear of ARC comes from the unknown.
Here are some of the questions I was asked when I tried to convince the team to use ARC:
ARC, like Java GC, incurs a performance penalty?
First of all, ARC and GC are two different things, ARC is a compile-time compiler that “helps you” insert memory management code that you would otherwise write by hand, rather than a garbage collection system that runs like GC
ARC memory does not know when to free, resulting in uncontrolled memory fluctuations?
If you understand how ARC works, you know that the memory management code inserted by the compiler under ARC is optimized so that no extra line of code is wasted, so to speak, handwritten memory management must be very rigorous to be as complete and seamless as ARC automatically generates
ARC below their own memory management, very uncomfortable, very insecure
This is purely a matter of habit. The developer’s goal is to achieve the most reliable application in the simplest way possible, and progress needs to change. Fortunately, -fobjc-arc and -fno-objc-arc are provided in the compile options to ensure that the whole transformation continues, just like Hong Kong and Macao in socialist China
The provisions of the ARC
One of the confusing aspects of using ARC is that if an object is generated by a method and there is no tag attached, how does ARC know if the object is an AutoRelease?
@interface Sark : NSObject+ (instancetype)sarkWithMark:(NSString *)mark; // 1- (instancetype)initWithMark:(NSString *)mark; // 2@end
Copy the code
This is a common non-Arc trick, 1 generates an AutoRelease object, 2 generates a normal object, and now ARC can’t call autoRelease, how do you know when you use it?
{/ /... Sark *sark1 = [Sark sarkWithMark:@" sarkWithMark "]; Sark *sark2 = [[Sark alloc] initWithMark:@" Sark "]; }Copy the code
Using convention, NS defines the following three compilation properties
#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))#define NS_RETURNS_INNER_POINTER __attribute__((objc_returns_inner_pointer))
Copy the code
These three attributes are used by Clang himself, and should not be used except in special cases, but they are useful for understanding ARC. Here is another concept, Method family
An Objective-C method may fall into a method family, which is a conventional set of behaviors ascribed to it by the Cocoa conventions.
Refers to the name represents a type of method, for example – init and – initWithMark: belong to the family of the init as a result, the compiler convention, to alloc, init, copy, mutableCopy, the new method of this a few family, NS_RETURNS_RETAINED identifier is added by default. Other methods that don’t name family add NS_RETURNS_NOT_RETAINED identifier by default
Ps. IOS development exchange technology group: welcome to join, no matter you are big or small white welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning and growth together
The compiler looks like this:
@interface Sark : NSObject+ (instancetype)sarkWithMark:(NSString *)mark NS_RETURNS_NOT_RETAINED; // 1- (instancetype)initWithMark:(NSString *)mark NS_RETURNS_RETAINED; // 2@end
Copy the code
That’s why you can’t define an attribute with a name like this under ARC:
@property (nonatomic, copy) NSString *newString; // The compiler does not allow itCopy the code
NS_RETURNS_INNER_POINTER is used when an object returns an internal C pointer, such as the NSString method:
- (__strong const char *)UTF8String NS_RETURNS_INNER_POINTER;
Copy the code
Using this logo, I won’t go into this further, but go directly to the document:
An Objective-C method returning a non-retainable pointer may be annotated with the objc_returns_inner_pointer attribute to indicate that it returns a handle to the internal data of an object, and that this reference will be invalidated if the object is destroyed. When such a message is sent to an object, the object’s lifetime will be extended until at least the earliest of:
the last use of the returned pointer, or any pointer derived from it, in the calling function or
the autorelease pool is restored to a previous state.