pkc456
pkc456

Reputation: 8506

Return multiple values from a function in objective C

How do I return two or more separate data values of the same/different type from a method in Objective-C?

I think I just don't understand the syntax for returning multiple values.

Below is the code I'm using in swift, I'm having trouble with the objective -C version.

func getData() -> (Int, Int, Int) {
    //...code here
    return ( hour, minute, second)
}

Upvotes: 0

Views: 3847

Answers (1)

Bilal
Bilal

Reputation: 19156

You can't do that in objective-c. Best option is using parameters by reference. Something like this.

- (void)getHour:(int *)hour minute:(int *)minute second:(int *)second {
    *hour = 1;
    *minute = 2;
    *second = 3;
}

And use it like this.

int a,b,c;
[self getHour:&a minute:&b second:&c];
NSLog(@"%i, %i, %i", a, b , c);

Upvotes: 10

Related Questions