开发者

Add objects in a NSMutableArray declared in another function

Ideally, I would like to make a function add objects in a NSMutableArray, and then do whatever I want with this array in another function.

Here is what I've tried to do lately, of course it doesn't work but it gives you an idea of what I want to do:

- (void)someThing 
{  
   (..开发者_JS百科.)

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

while (theObject = [aNSEnumerator nextObject]) {
   const char *theObject_fixed = [theObject UTF8String]; 

   function_something(theObject_fixed);
}

// do something with arrayOfThings


}



void function_something(const char *file)
{

    (...)


unsigned int *p = memmem(buffer, fileLen, bytes, 4);

NSMutableString *aString = [[NSMutableString alloc] initWithCapacity:48];

unsigned long off_to_string = 0x10 + 4 + ((void *)p) - ((void *)buffer);

for (unsigned long c = off_to_string; c<off_to_string+0x30; c++)
{

[aString appendFormat:@"%.2x", (int)buffer[c]];

}

 NSLog(@"%s: %@", file, aString);   

[arrayOfThings addObject:[aString copy]];

 free(buffer);


There are two ways to go about this:

The first requires only a slight modification to your code will allow you to do what you want: In the funciton someThing pass the mutable array as an additional parameter.

function_something(theObject_fixed, arrayOfThings);

Then change function_something to accept that parameter.

void function_something(const char *file, NSMutableArray *arrayOfThings) {
    // Code remains the same
}

The other and in my opinion better solution would be for the function_something to return the fixed string as an NSString object and let someThing do the adding to the mutable array. So we get something like this in someThing:

...
NSString *aString = function_something(theObject_fixed);
[arrayOfThings addObject:aString];

And then a redefined *function_something*:

NSString* function_something(const char *file) {
    ...
    return [aString autorelease];
}

By the way, your code is leaking memory. Be careful with you retain/release/autorelease.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜