views:

111

answers:

2

I have a method that takes an NSMutableArray as a parameter, and I would like it to return that array, another array that is created in the method, and an int created by the method. I realize this could be done by making an array that had all these objects in it, and returning that, then removing them from the array outside the method, but is there another way to return multiple objects?

A: 

Another way you can do it is have the user pass a pointer which they want to use to hold the data. Here is a sample that returns an array and uses pointers to give you an int value and another array. EDIT ok here is the working version I just tested it myself:

- (NSMutableArray*)doStuff:(int*)container returnedArray:(NSMutableArray*)arrayContainer{
   int a = 4;
   *container = a;
   [arrayContainer removeAllObjects];
   [arrayContainer addObject:@"object"];
   return [NSMutableArray arrayWithObjects:@"object",nil];
}

That way you could say like:

int value = 0;
NSMutableArray* new = [NSMutableArray array];
[self doStuff:&value returnedArray:new];

And it basicly works like a return!

Justin Meiners
Although the general technique you describe is one that's used in real programs, that code is totally broken and will not do what is intended at all. You seem to have some misconceptions about how pointers work, and also about variable scope.
Chuck
Yeah, I thought this seemed a little off. Thanks for the idea though.
Regan
@Chuck I didn't test it I was just giving a sample of the concept and I wrote that in very little time. I was just trying to get him in the right direction.
Justin Meiners
@Chuck some of my wording might be a little off but the basics should work.
Justin Meiners
@Regan like chuck said it really does work you might just want to find a better example...
Justin Meiners
It isn't that your "wording is a little off." It's that your code is completely wrong. The only thing it does correctly is return an NSMutableArray containing the string `@"object"`. The type of the `arrayContainer` variable is wrong and the `container` pointer is never dereferenced, so it can't affect anything outside of your function.
Chuck
@Chuck ok I will give an actual example I use.
Justin Meiners
@Chuck @Regan Ok I fixed it and checked it myself. Even chuck will agree with me this time.
Justin Meiners
That'll "work", but if I ever found code like that in something I was working on, I'd immediately rewrite it. ;)
bbum
@bbum Far enough I wouldm't do the array thing either but I do like the integer one.
Justin Meiners
typo far = fair
Justin Meiners
+2  A: 

Using an NSDictionary to return multiple values is the customary way to do this in Obj-C.

The method signature would look something like this:

-(NSDictionary *)doSomeStuffThatReturnsMultipleObjects;

and you are going to want to define your dictionary keys in the appropriate files.

// Header File
extern NSString *const JKSourceArray;
extern NSString *const JKResultsArray;
extern NSString *const JKSomeNumber;

// Implementation File
NSString *const JKSourceArray = @"JKSourceArray";
NSString *const JKResultsArray = @"JKResultsArray";
NSString *const JKSomeNumber = @"JKSomeNumber";

The advantage over using an array is that the the order of elements and the presence/absence of elements doesn't matter, making it far easier to extend in the future, if you want to return additional objects. It's also far more flexible and extensible than passing by reference.

kubi