ARC prohibits abstract

New to ios and trying to return an NSString from a function. As far as I understand, I need [NSString... alloc] init] to create a string to return. Also, since I called alloc , I assume that I need to auto-update the object myself, but I get the message "ARC denies explicit send message" autorelease ", so .. how do I tell ios when I finished with what NSString is highlighted?

 - (NSString *) generateUID { char foo[32]; // create buffer of all capital psuedo-random letters for (int i = 0; i < sizeof(foo); i++) foo[i] = (random() % 25) + 65; // 0-25 + CAPITAL_A NSString *uid = [[NSString alloc] initWithBytes:foo length:sizeof(foo) encoding:NSASCIIStringEncoding]; NSLog (@"uid: %@", uid); return (uid); } 
0
ios iphone nsstring
source share
4 answers

ARC = automatic reference counting = compiler adds the necessary releases and startups based on the analysis of your code. Of course, you donโ€™t see this, because it happens behind the scenes. As mentioned in your sosbom comment, you really should read the applicable documentation on the Apple website .

+8
source share

Not.

autorelease exists only for compatibility, before iOS 5, you will need to do:

 Thing *myThing = [[Thing alloc] init]; // Retain count: 1 [myArray addObject:myThing]; // Retain count: 2 [myThing release]; // Retain count: 1 

With the above code, the responsibility for saving the object is transferred to the array, when the array is deleted, it frees its objects.

Or in the case of auto-advertising.

 - (MyThing*)myMethod { return [[[MyThing alloc] init] autorelease]; } 

Then it will free the object after loading it into NSAutoReleasePool and delete it after deleting it.

ARC will take care of this now, it pretty much inserts the missing parts for you, so you donโ€™t have to worry about it, and the beauty of this is that you get the benefits of link counting (versus garbage collector), due to a higher compile time check to complete the ARC step, but end-users don't care about compile time.

Add to this that you now have strong and weak (vs their brothers without ARC retain and assign , the later version is still useful for non-saved things), and you get a good programming experience without tracing the code with your eyes and counting the number of deductions in the left hand.

+3
source share

Short answer: no! ARC will handle memory management for you. When ARC is turned on, the compiler inserts the appropriate memory management statements, such as save and release messages.

It is best to use ARC because the compiler better understands the life cycle of an object and is less prone to human error.

Another important thing to note is ARC. ARC does not match traditional garbage collection. In the background, there is no separate process or thread, for example, java GC, which deletes objects when there is no longer a reference to them. You could think of ARC as compile-time garbage collection :).

The only thing you need to know is the reference loops and pointers / bridge objects between Objective-C and Obj-C ++ / C. You might want to find http://en.wikipedia.org/wiki/Weak_reference

Hope this helps

+1
source share

In general, you should define a constructor method in your class and put the distribution logic in this method. Then it is much more difficult to make a type selection error, since the alloc / init approach always returns (id) and is not very type safe. These are, for example, built-in classes such as NSString, for example [NSString stringWithString: @ "foo"]. Here is a small block that you can use to write code that works with both the old non-archetypal compilation and arc support.

 + (AVOfflineComposition*) aVOfflineComposition { AVOfflineComposition *obj = [[AVOfflineComposition alloc] init]; #if __has_feature(objc_arc) return obj; #else return [obj autorelease]; #endif // objc_arc } 

Then you declare the method and instantiate the object as follows:

 AVOfflineComposition *obj = [AVOfflineComposition aVOfflineComposition]; 

Using the above approach is best, as it is safe in type and same code with vs non-arc arc. The compiler will complain if you try to assign the returned object to a variable of another type.

0
source share

All Articles