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);
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