Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

apffal

macrumors newbie
Original poster
Feb 10, 2008
24
0
I know it's possible to return two or more values from a function using Cocoa - Objective C.
Can someone give me a code example, for getting integer and/or NSString values ?
Thanks in advance
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You can only return a single value, either a primitive type or an object from a function. But using an object you can return multiple values by encapsulating them in the object. The simplest method would be to return a NSDictionary using pre-defined keys.

Code:
#define STRING_KEY @"string"
#define NUMBER_KEY @"number"

-(NSDictionary *) myMethod
{
....
NSMutableDictionary *toReturn = [NSMutableDictionary dictionary];
[toReturn setObject:myNSString forKey:STRING_KEY];
[toReturn setObject:myNSNumber forKey:NUMBER_KEY];
return toReturn;
}

....

NSDictionary *return = myMethod;
stringReturned = [return objectForKey:STRING_KEY];
nsNumberReturned = [return objectForKey:NUMBER_KEY];
 

apffal

macrumors newbie
Original poster
Feb 10, 2008
24
0
Gives me syntax errors, before "return", on three last lines of code.
What's wrong ?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I've no idea: I just typed it straight into here: you're meant to look at it, understand the concept and apply that to your problem, not simply copy and paste it: that's not programming at all.
 

apffal

macrumors newbie
Original poster
Feb 10, 2008
24
0
I've used the code like this

Code:
#import "Calc.h"
#define STRING_KEY @"string"

@implementation Calc

-(NSDictionary *) calcDias {

-----------
NSMutableDictionary *toReturn = [NSMutableDictionary dictionary];
[toReturn setObject:final_1 forKey:STRING_KEY];
[toReturn setObject:final_2 forKey:STRING_KEY];
return toReturn;
}

- (IBAction)calc:(id)sender { 

calc = [[Calc alloc]init]; 	

NSDictionary *return = calcDias;
final_1 = [return objectForKey:STRING_KEY];
final_2 = [return objectForKey:STRING_KEY];

--------
}

and get the syntax errors on these 3 lines

Code:
NSDictionary *return = calcDias;
final_1 = [return objectForKey:STRING_KEY];
final_2 = [return objectForKey:STRING_KEY];

I' ve defined "final_1" and "final_2" as NSString.
Can you help ?
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
I've used the code like this

Code:
#import "Calc.h"
#define STRING_KEY @"string"

@implementation Calc

-(NSDictionary *) calcDias {

-----------
NSMutableDictionary *toReturn = [NSMutableDictionary dictionary];
[toReturn setObject:final_1 forKey:STRING_KEY];
[toReturn setObject:final_2 forKey:STRING_KEY];
return toReturn;
}

- (IBAction)calc:(id)sender { 

calc = [[Calc alloc]init];     

NSDictionary *return = calcDias;
final_1 = [return objectForKey:STRING_KEY];
final_2 = [return objectForKey:STRING_KEY];

--------
}
and get the syntax errors on these 3 lines

Code:
NSDictionary *return = calcDias;
final_1 = [return objectForKey:STRING_KEY];
final_2 = [return objectForKey:STRING_KEY];
I' ve defined "final_1" and "final_2" as NSString.
Can you help ?

What is the syntax error you are getting? It should be pretty clear from the error what the problem is if you sit down and think about it. Better start learning how to debug your code now, it is an important skill to have :).
 

apffal

macrumors newbie
Original poster
Feb 10, 2008
24
0
The error I get ("syntax error before return") is not exactly very clear and explicit - and if I could understand it by myself, certainly would not ask more questions.
Thank you, anyway
 

yoavcs

macrumors regular
Apr 7, 2004
220
96
Israel
I'd think you'd need something like:

NSDictionary *return = [self calcDias];

seeing as how calcDias is an instance method
 

ldmbouge

macrumors newbie
Jan 4, 2009
15
7
I've used the code like this

Code:
#import "Calc.h"
#define STRING_KEY @"string"

@implementation Calc

-(NSDictionary *) calcDias {

-----------
NSMutableDictionary *toReturn = [NSMutableDictionary dictionary];
[toReturn setObject:final_1 forKey:STRING_KEY];
[toReturn setObject:final_2 forKey:STRING_KEY];
return toReturn;
}

- (IBAction)calc:(id)sender { 

calc = [[Calc alloc]init]; 	

NSDictionary *return = calcDias;
final_1 = [return objectForKey:STRING_KEY];
final_2 = [return objectForKey:STRING_KEY];

--------
}

and get the syntax errors on these 3 lines

Code:
NSDictionary *return = calcDias;
final_1 = [return objectForKey:STRING_KEY];
final_2 = [return objectForKey:STRING_KEY];

I' ve defined "final_1" and "final_2" as NSString.
Can you help ?


Easy... you can't call a local variable "return". "return" is a reserved word in C...

Hope that helps,

--
Laurent
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.