method with 2 return values

后端 未结 7 713
春和景丽
春和景丽 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 16:17

    I see everyone has mentioned an NSArray but I'd go with an NSDictionary so the values don't have to be added in order or even at all. This means it is able to handle a situation where you only want to return the second string.

    - (NSDictionary*)myFunction {
    
       NSString *myString1 = @"string1";
       NSString *myString2 = @"string2";
    
       return [NSDictionary dictionaryWithObjectsAndKeys: myString1, @"key1", myString2, @"key2", nil];
    }
    
    NSDictionary *myDictionary = [self myFunction]
    NSString *string1 = [myDictionary objectForKey:@"key1"];
    NSString *string2 = [myDictionary objectForKey:@"key2"];
    

提交回复
热议问题