0

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 2

5

It sounds like you're looking for something like this:

NSMutableArray *array = [[NSMutableArray alloc] init];

while(foo) {
    // create your string
    [array addObject:string];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to release or autorelease the array.
0
-(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

[outArr copy] will leak, and still return a mutable array
-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.