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?
+5
A:
It sounds like you're looking for something like this:
NSMutableArray *array = [[NSMutableArray alloc] init];
while(foo) {
// create your string
[array addObject:string];
}
Can Berk Güder
2009-03-17 12:08:01
Don't forget to release or autorelease the array.
Peter Hosey
2009-03-17 13:43:48
A:
-(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
}
kent
2009-03-17 12:16:45
-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.
Peter Hosey
2009-03-17 13:43:10