Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple values from a method in Objective-C

I asked a similar question, but I couldn't get it working exactly. I'm building an iPhone app, and there is a method that I want called from different files. I figured the easiest way would simply be to make a method in another file, and call the method from the other files.

Here are some problems. I need to return multiple values from the method, after passing it multiple values. For example, I'm passing it: (int, int, int, string, string). And it needs to return all of those values, after they have been changed. Someone showed me this code:

- (NSDictionary *)EndOfTurn:(int)varTurns withFatness:(int)varFatness {     varTurns--;      if (varTurns <= 0) {         varFatness = varFatness - 5;     }     else {         varFatness += 2;     }      return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:varFatness], @"FATNESS", [NSNumber numberWithInt:varTurns], @"TURNS", nil];  } 

However, this code doesn't work, and I need some more information to really understand it. Let's assuming I'm passing it these values:

int varMoney; int varNumSheep; int varNumShepherds; NSString *test1; NSString *test2; 

So I need to get all of these values back from the method.

How do I declare this in the header file? This should be in an Objective-C file, but could you give me the code for the entire file so I can see where it would go with the @implementation and @end, whatnot. Also, how would I call this method?

like image 904
Ethan Mick Avatar asked Nov 07 '09 04:11

Ethan Mick


People also ask

Can you return multiple values in a method?

You can return only one value in Java. If needed you can return multiple values using array or an object.

Can you return multiple values in a function in C?

In C or C++, we cannot return multiple values from a function directly.

Can you return multiple values from a function using return statement?

No, you can not return multiple values like this in C. A function can have at most one single return value.

What are the two ways of returning multiple values from function?

Below are the methods to return multiple values from a function in C: By using pointers. By using structures. By using Arrays.


2 Answers

What about passing in the values as pointers?

For example:

- (void) getValuesForInt:(int *)int1 anotherInt:(int *)int2 aBool:(BOOL *)bool1 anotherBool:(BOOL *)bool2 {   if (*int1 == 42 && *int2 == 0) {     *int1 = 0;     *int2 = 42;   }   if (*bool1 == NO) {     *bool2 = YES;   } } 

Then you can invoke it like:

int int1 = 42; int int2 = 0; BOOL bool1 = NO; BOOL bool2 = NO; [self getValuesForInt:&int1 anotherInt:&int2 aBool:&bool1 anotherBool:&bool2]; NSLog(@"int1: %d int2: %d bool1: %d bool2: %d", int1, int2, bool1, bool2); //prints "int1: 0 int2: 42 bool1: 0 bool2: 1" 

Edit:

This works equally well with objects. You'll often see this used when dealing with NSError objects:

NSError *error = nil; [anObject doSomething:foo error:&error]; 

Can be implemented as:

- (void) doSomething:(id)terrible error:(NSError **)error {   if ([terrible isEqual:reallyBad]) {     if (error != nil) { *error = [NSError errorWithDomain:@"domain" code:42 userInfo:nil]; }   } } 
like image 95
Dave DeLong Avatar answered Sep 21 '22 23:09

Dave DeLong


You can use a block closure to pass back multiple values from a method like this. -rrh

[self heyFunctionGiveMeBackTwoValuesFromThisFruitArray:@[@"apple", @"orange", @"banana", @"apple"] findThisFruit:@"apple" closureFunction:^(int fruitCount, NSString* fruitString) {     NSLog(@"Two values returned, int-fruitCount:%d, NSString-fruiteString:%@", fruitCount, fruitString); }];  - (void)heyFunctionGiveMeBackTwoValuesFromThisFruitArray:(NSArray*)fruitsArray findThisFruit:(NSString*)findThisFruit closureFunction:(void (^)(int fruitCount, NSString *fruitString))passBackResultsUsingThisClosure {     NSInteger fruitsFound = 0;     NSString* fruitsMessage = [NSString stringWithFormat:@"No %@ Found", findThisFruit];     for (NSString* string in fruitsArray)     {         if ([string compare:findThisFruit] == NSOrderedSame)         {             fruitsFound++;         }     }     if (fruitsFound > 0)     {         fruitsMessage = [NSString stringWithFormat:@"You have %@ on your list this many times:%d", findThisFruit, fruitsFound];     }     passBackResultsUsingThisClosure(fruitsFound, fruitsMessage); } 

Results: Two values returned, int-fruitCount:2, NSString-fruiteString:You have apple on your list this many times:2

like image 40
Richie Hyatt Avatar answered Sep 20 '22 23:09

Richie Hyatt