I have a little foundation tool test (Objective-C) that I am playing with and I have a few questions …
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int planetLoop;
NSString *tempString;
NSMutableArray *planetArray = [[NSMutableArray alloc] init];
NSLog(@"STRING ARRAY ... Start");
for(planetLoop=0; planetLoop<10; planetLoop++) {
tempString = [NSString stringWithFormat: @"Planet_%03d", planetLoop+1];
NSLog(@"Planet_%03d", planetLoop+1);
[planetArray addObject:tempString];
}
[planetArray release];
[pool drain];
return 0;
}
First, usually I release an object after adding it to an array, but am I right in thinking that what I have currently is right because “tempString” is a string literal, and as such does not need to be allocated or released?
Secondly, when I run this (prior to execution) I get the following eror “unable to read unknown load command 0x80000022” if this a problem with my code? from searching on google it looks like it might be a bug in xCode 3.1.2, anyone have any ideas?
Finally anything I am doing wrong, the idea is to fill an array with 10 string “Planet_001” through to “Planet_010”
EDIT: Ah, I see, thats because of the “= [NSString” bit i.e.
// Autoreleased object
tempString = [NSString stringWithFormat: @"Planet_%03d", planetLoop+1];
// String literal
tempString = @"Planet_";
many thanks, much appreciated -gary-
tempStringisn’t actually a string literal.@"Planet_%03d"is a string literal.tempStringis an autoreleased object, meaning that it will be released when the NSAutoreleasePool is drained. Basically, the memory is already managed and you don’t have to do anything.The rule is: If you
new,alloc,copyorretainan object, then you have toreleaseit. Otherwise, the memory is already managed, probably by an autorelease.Also, you forgot to releasepool. Other than that, it looks fine.