Question

I'm working on an iPhone app using objective C. I've got class A, which creates an NSMutableArray pointer called "list". But, in class A, I never create an object for it to point to. Instead, I call a class method in class B, to get some data from an sqlite database, and populate an NSMutableArray object there. I want to be able to set the pointer in class A to point to the NSMutableArray object created in the class B method by passing it as an argument to the method. I can't do it by returning the array because I want to return the sqlite result.

I wonder if I'm doing this right? I haven't written the entire method yet (when it's done it'll be pretty long), but I want to know if I'm doing the pointer stuff correctly before I get started on the rest.

//ClassA.m

//...
NSMutableArray *list;
[ClassB populateArrayFromSqlite:&list];
//Now do stuff with "list", which should point to the array created in populateArrayFromSqlite
//...

//ClassB.m
+(int)populateArrayFromSqlite:(NSMutableArray**)ar {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    //Here there will be code that populates the array using sqlite (once I write it)
    //And then I need to set the "list" pointer in ClassA.m to point to this array
    ar = &array; //Is this how?

    return resultFromSqlite;
}

Did I do it right? Or am I not understanding something? I guess this pointer-to-pointer stuff just doesn't click with me yet. After reading a few general sources about pointers, I suspect this is how I'd do it but part of me doesn't understand why the ar argument can't just be a regular pointer (as opposed to a pointer-to-pointer).

Was it helpful?

Solution

Pointers to pointers are a bit escheric, yes. The simple way to do it would be to create an empty array in A and pass a regular array pointer to B that would just fill it. If you insisted on creating the array in B, I think you could do this:

- (void) createArray: (NSMutableArray**) newArray
{
    NSAssert(newArray, @"I need a pointer, sweetheart.");
    *newArray = [[NSMutableArray alloc] init];
    [*newArray addObject:...];
}

OTHER TIPS

Changing pointers to objects like this is fairly uncommon in Objective-C. The main time we see stuff like this is when a method has a potential for failure, at which point we'll pass in a pointer to an NSError reference. For example, NSFileManager's - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error.

The far more common way to do this would be to have the method return the array:

NSMutableArray * list = [ClassB mutableArrayFromSQLite];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top