views:

891

answers:

2

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
Don't forget to release or autorelease the array.
Peter Hosey
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
[outArr copy] will leak, and still return a mutable array
cobbal
-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