Reputation: 207
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?
Upvotes: 1
Views: 6645
Reputation: 34992
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");
}
}
Upvotes: 1
Reputation: 8069
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
Upvotes: 2
Reputation: 77201
As Jules points out a method can "return" only a single value. However, you have several options for returning multiple values:
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.
Pass multiple pointers in your parameters, and the method can store results in the object or variable pointed to. See example here.
Return a struct that has multiple fields. There's an example here.
Upvotes: 2
Reputation: 7223
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];
Upvotes: 5
Reputation: 9544
You will have to return an NSArray or NSDictionary with the two values.
Upvotes: 0