1

I am new to Objective-C. I have written a function that returns two values. Now I would like to print it into two separate labels, how I can do it?

-(NSString *)abc:(NSInteger)weeks year:(NSInteger)year{

............

return ca , da ;

}

and when I call this function like

resultLabel1.text = [self abc year:year];  //output show the value of da

now I want to show the value of ca into resultLabel1.text and da into resultLabel2.text

is it possible?

Ashish
  • 207
  • 1
  • 3
  • 19

5 Answers5

5

You can only return a single value from any method in C and C-derived languages. So you simply need to return a single value that represents both values. You can achieve this by making use of a NSDictionary.

So make it:

-(NSDictionary *)abc:(NSInteger)weeks year:(NSInteger)year{

     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
           ca, @"object1", 
           da, @"object2", nil];

    return dict;

}

Another way is to return an NSArray:

- (NSArray *)abc:(NSInteger)weeks year:(NSInteger)year {

     NSArray *myArray = [NSArray arrayWithObjects:da, ca, nil];
     return myArray;
}

And you can then use these values as:

NSArray *myArray = [self abc:2 year:2004];
textLabel.text = (NSString *)[myArray objectAtIndex:0];
textLabel2.text = (NSString *)[myArray objectAtIndex:1];
Jules
  • 7,148
  • 6
  • 26
  • 50
  • Hello i have used it but its show warning (NSString does not response at objectAtIndex, objective c ) both return value show string so how i can fixed this issue. – Ashish Aug 27 '11 at 14:42
  • I think you did not change your return type of your method. It is thinking you are returning a NSString which obviously does not respond to objectAtIndex. Make sure you return and receive a NSArray. That object **does** respond to objectAtIndex. – Jules Aug 27 '11 at 15:22
2

As Jules points out a method can "return" only a single value. However, you have several options for returning multiple values:

  1. Return a pointer to an object, where the object contains multiple values. The object can be an NSArray, NSDictionary or you own class. Jules answer gave some examples of this.

  2. Pass multiple pointers in your parameters, and the method can store results in the object or variable pointed to. See example here.

  3. Return a struct that has multiple fields. There's an example here.

Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
2

I'd use a NSDictionary to return multiple values from a method. In the dictionary each value is named and referenced by a key. The keys in this example are "ca" and "da" and the values are both a short string of text.

-(NSDictionary *) abc: (NSInteger) weeks year:(NSInteger) year {

   NSString* ca = [NSString stringWithFormat:@"Week is %d", weeks];
   NSString* da = [NSString stringWithFormat:@"Year is %d", year];

   return [[NSDictionary alloc] initWithObjectsAndKeys:ca, @"ca", da, @"da", nil];
}

Call the method and pick out the returned values with code like this:

   NSInteger weekParam = @"52".integerValue;
   NSInteger yearParam = @"2011".integerValue;

   NSDictionary *result = [self abc:weekParam  year:yearParam];

   NSLog(@"ca has value: %@", [result objectForKey:@"ca"]);
   NSLog(@"da has value: %@", [result objectForKey:@"da"]);

You log should have the following lines added:

ca has value: Week is 52
da has value: Year is 2011
Niels Castle
  • 8,039
  • 35
  • 56
1

You can "return" multiple objects as parameters in a block:

- (void)method {
    [self returnMultipleObjectsWithCompletion:^(NSString *firstString, NSString *secondString) {
        NSLog(@"%@ %@", firstString, secondString);
    }];
}

- (void)returnMultipleObjectsWithCompletion:(void (^)(NSString *firstString, NSString *secondString))completion {
    if (completion) {
        completion(@"firstReturnString", @"secondReturnString");
    }
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
0

You will have to return an NSArray or NSDictionary with the two values.

Dancreek
  • 9,524
  • 1
  • 31
  • 34