method with 2 return values

后端 未结 7 710
春和景丽
春和景丽 2021-01-24 15:48

I want to call a method which returns two values

basically lets say my method is like the below (want to return 2 values)

NSString* myfunc
{
   NSString          


        
相关标签:
7条回答
  • 2021-01-24 15:58

    You have a few options:

    • NSArray: Just return an array. Pretty simple.
    • Pointers: Pass in two pointers, and write to them instead of returning anything. Make sure to check for NULL!
    • Structure: Create a struct that has two fields, one for each thing you want to return, and return one of that struct.
    • Object: Same a structure, but create a full NSObject subclass.
    • NSDictionary: Similar to NSArray, but removes the need to use magic ordering of the values.
    0 讨论(0)
  • 2021-01-24 16:00

    As you can only return one value/object, maybe wrap them up in an array:

    -(NSArray*) arrayFromMyFunc
    {
       NSString *myString = @"MYDATA";
       NSString *myString2 = @"MYDATA2";
       return [NSArray arrayWithObjects:myString,myString2,nil];
    }
    

    You can then use it like this:

    NSArray *arr = [self arrayFromMyFunc];
    
    NSString *value1 = [arr objectAtIndex:0];
    NSString *value2 = [arr objectAtIndex:1];
    

    You could pass results back by reference, but this is easy to get wrong (syntactically, semantically, and from memory management point of view).

    Edit One more thing: Make sure that you really need two return values. If they are quite independent, two separate function are often the better choice - better reusabilty and mentainable. Just in case you are making this as a matter of premature optimization. :-)

    0 讨论(0)
  • 2021-01-24 16:00

    Wrap the two strings in an NSArray:

    - (NSArray*)myFunc
    {
       NSString *myString = @"MYDATA";
       NSString *myString2 = @"MYDATA2";
       return [NSArray arrayWithObjects:myString, myString2, nil];
    }
    
    NSArray *theArray = [self myFunc]
    NSString *value1 = [theArray objectAtIndex:0];
    NSString *value2 = [theArray] objectAtIndex:1];
    
    0 讨论(0)
  • 2021-01-24 16:01

    You can only return 1 value. That value can be a struct or an object or a simple type. If you return a struct or object it can contain multiple values.

    The other way to return multiple values is with out parameters. Pass by reference or pointer in C.

    Here is a code snippet showing how you could return a struct containing two NSStrings:

    typedef struct {
        NSString* str1;
        NSString* str2;
    } TwoStrings;
    
    TwoStrings myfunc(void) {
        TwoStrings result;
        result.str1 = @"data";
        result.str2 = @"more";
        return result;
    }
    

    And call it like this:

    TwoStrings twoStrs = myfunc();
    NSLog(@"str1 = %@, str2 = %@", twoStrs.str1, twoStrs.str2);
    

    You need to be careful with memory management when returning pointers even if they are wrapped inside a struct. In Objective-C the convention is that functions return autoreleased objects (unless the method name starts with create/new/alloc/copy).

    0 讨论(0)
  • 2021-01-24 16:01

    You can only directly return one value from a function. But there is a way of doing it.

    -(void) myfuncWithVal1:(NSString**)val1 andVal2:(NSString**)val2
    {
      *val1 = @"MYDATA";
      *val2 = @"MYDATA2";
    }
    

    Then to call it outside the method you'd use:

    NSString* a;
    NSString* b;
    
    [self myfuncWithVal1:&a andVal2:&b];
    
    0 讨论(0)
  • 2021-01-24 16:12
    void myfunc(NSString **string1, NSString **string2)
    {
        *string1 = @"MYDATA";
        *string2 = @"MYDATA2";
    }
    
    ...
    
    NSString *value1, *value2;
    myfunc(&value1, &value2);
    

    Remember that you need to pass a pointer to a pointer when working with strings and other objects.

    0 讨论(0)
提交回复
热议问题