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).
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:...];
}