Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5 NSString release not available

I read on the web that when I create an object with alloc and init I have to release it (even a NSString), so:

Why if I create a NSString this way:

NSString *prova = [[NSString alloc] init];
[prova release];

I get these errors:

'release' is unavailable: not available in automatic reference counting mode

and

ARC forbids explicit message send of 'release'

on the [prova release] message? I get no error when I try do this:

NSString *prova = [[NSString alloc] init];
NSLog(@"Contenuto 0 di prova: %@", prova);
prova = @"prima prova stringa";
NSLog(@"Contenuto 1 di prova: %@", prova);
prova = @"ma cosè questo fantomatico errore";
NSLog(@"Contenuto 2 di prova: %@", prova);
like image 481
iLeW Avatar asked Feb 10 '26 09:02

iLeW


2 Answers

That's a best practice previous to iOS 5, or in iOS 5 if ARC mode is disabled. Now iOS 5 uses the new Apple's LLVM compiler, which introduces this ARC feature.

So if ARC is enabled (and it is by default), you do not need to use, in general, the release method anymore. You can find more details in documentation.

If you still want to develop the old way, you can add the flag -fno-objc-arc in "Build phases" section of a Xcode project

like image 131
elitalon Avatar answered Feb 13 '26 00:02

elitalon


You are using Apple's new ARC (automatic reference counting). ARC is a new compiler function which adds retain, release and autorelease on compile time automatically.

Have a look at the iOS 5 Release Nodes for more information about ARC: http://developer.apple.com/technologies/ios5/

Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

like image 43
miho Avatar answered Feb 13 '26 02:02

miho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!