Multiple values from a function with Cocoa - Obj C

Discussion in 'Mac Programming' started by apffal, Jan 4, 2009.

  1. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #1
    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
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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];
    
     
  3. apffal thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #3
    Gives me syntax errors, before "return", on three last lines of code.
    What's wrong ?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. apffal thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #5
    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 ?
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    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 :).
     
  7. apffal thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #7
    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
     
  8. yoavcs macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #8
    I'd think you'd need something like:

    NSDictionary *return = [self calcDias];

    seeing as how calcDias is an instance method
     
  9. ldmbouge macrumors newbie

    Joined:
    Jan 4, 2009
    #9

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

    Hope that helps,

    --
    Laurent
     
  10. cazlar macrumors 6502

    Joined:
    Oct 2, 2003
    Location:
    Sydney, Australia
    #10
    Also, I may be wrong, but aren't you storing both final_1 and final_2 under the same key? So really you should have 2 keys!
     

Share This Page