I have created a while-loop in which temporary strings are created (string is updated everytime that loop performs). How can I create an array out of these temporary strings?
2 Answers
It sounds like you're looking for something like this:
NSMutableArray *array = [[NSMutableArray alloc] init];
while(foo) {
// create your string
[array addObject:string];
}
1 Comment
Peter Hosey
Don't forget to release or autorelease the array.
-(NSArray*) makeArray
{
NSMutableArray* outArr = [NSMutableArray arrayWithCapacity:512]; // outArr is autoreleased
while(notFinished)
{
NSString* tempStr = [self makeTempString];
[outArr addObject:tempStr]; // will incr retain count on tempStr
}
return [outArr copy]; // return a non-mutable copy
}
2 Comments
cobbal
[outArr copy] will leak, and still return a mutable array
Peter Hosey
-copy won't necessarily return a mutable array; it should be immutable. -mutableCopy definitely would return a mutable array. [[copy] autorelease] is the correct way.