Passing a new structure as a parameter

I have a function like this:

- (void)addBalloon:(COLOR)color:(VELOCITY)velocity:(LOCATION)location 

Where COLOR is an enumeration, and VELOCITY and LOCATION are the structures defined in the constant header file.

VELOCITY and LOCATION store two ints, x and y.

When calling this method, I would call it like this:

 VELOCITY vel; LOCATION loc; vel.x = 100.0; vel.y = 0.0; loc.x = 10.0; loc.y = 10.0; [self addBalloon:Red:vel:loc]; 

But to me it seems disorganized. I would like to call a function directly on one line when creating a structure on a line.

Here is my question: I'm not sure if this can be done with #define .., but if that is not possible, is this the only other viable option to create a function that returns VELOCITY or LOCATION and accepts input x, and y?

I would like to do something like the following:

 [self addBalloon:Red:VELOCITY(100.0, 0.0):LOCATION(10.0, 10.0)]; 
+7
source share
1 answer

The C99 syntax for designated initializers can be used:

 [self addBalloon:Red:(VELOCITY){100.0, 0.0}:(LOCATION){10.0, 10.0}]; 

or

 [self addBalloon:Red:(VELOCITY){.x=100.0, .y=0.0}:(LOCATION){.x=10.0, .y=10.0}]; 
+7
source

All Articles