0

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 
{  
   (...)

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);
0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I chose your second solution and it works perfectly ! By the way, could you explain me a bit why it's leaking ?
in the function function_something you are using alloc/init so the retain count is 1 on that string and it's never released.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.