Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davbeck

macrumors regular
Original poster
May 16, 2008
104
0
Reno, NV
I tried to make a const NSPoint using NSMakePoint but I got errors that it wasn't a compile time constant. Makes sense that you couldn't do it but how would I do it then?
 
Code:
const NSPoint x = {(CGFloat)1.0,(CGFloat)2.0};

For initializing a C struct, you list it's elements in a comma separated list in the order of its elements inside curly braces. In this case x will be 1.0, y will be 2.0.

-Lee
 
Is there any worry that the struct will be rearranged in a future version or is that not something that would happen.
 
It would be pretty sad if this happened, but it's definitely a risk. I don't believe there's a means to initialize a struct using the named members. I guess in this case as long as you were setting x and y to the same thing you'd always be safe. Otherwise... hm:
Code:
	NSPoint x = NSMakePoint((CGFloat) 2.0, (CGFloat)1.0);
	const NSPoint y = x;

-Lee

EDIT: Looks like there's a C99 feature, so I was wrong about not being able to initialize by named member:
Code:
const NSPoint z = {.x = (CGFloat)2.0, .y = (CGFloat) 1.0};
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.