CGRect syntax I haven't seen before
Asked Answered
F

3

27

I saw the syntax below in some example code and am not sure I understand it.

 CGRect imageRect = (CGRect){.size = baseImage.size};

Is this simply a shorthand way of initializing a CGRect equivalent to:

 CGRect imageRect = CGRectMake(0,0,baseImage.size.width, baseImage.size.height);

Is there any benefit to this syntax aside from slightly less typing?

Featurelength answered 6/2, 2013 at 7:28 Comment(2)
just like [NSNumber numberWithInt:1] is equal to @(1)Gastrocnemius
1) Yes 2) No, one disadvantage is spotty autocomplete thoughSomeday
S
44

That's C99 initializer syntax. You can use it with any structure.

The main advantage to an Objective-C is that it gives you some very Objective-C like syntax, where the fields are close to the values rather than implied by positioning. (That's not to say this is intentionally similar, or that it's the only advantage. But it is nice.)

It's sometimes slightly more typing, but I use it everywhere now.

Consider:

CGRect a = CGRectMake(a+c/2, b+d/2, c, d);

In order to understand this, you need to understand the order of the parameters. You also need to be able to catch the commas easily with your eyes. In this case, that's pretty easy, but if the expressions were more complicated you'd probably be storing them in a temporary variable first.

The C99 way:

CGRect a = (CGRect){
    .origin.x = a+c/2,
    .origin.y = b+d/2,
    .size.width = c,
    .size.height = d
};

It's longer, but it's more explicit. It's also very easy to follow what is assigned to what, no matter how long the expression are. It's also more like an Objective-C method. After all, if CGRect was a class, it would probably look like this:

CGRect *a = [[CGRect alloc] initWithOriginX:x originY:y width:w height:h];

You can also do things like this:

CGRect a = (CGRect){
    .origin = myOrigin,
    .size = computedSize
};

Here, you're building a rectangle using a CGPoint and CGSize. The compiler understands that .origin expects a CGPoint, and .size expects a CGSize. You've provided that. All's gravy.

The equivalent code would be CGRectMake(myOrigin.x, myOrigin.y, size.width, size.height). By using CGRectMake you're no longer expressing the same kind of meaning to the compiler. It can't stop you from assigning part of the size to the origin. It also won't stop you from assigning the width to the height. It doesn't even give you a good clue about which is the X and Y; if you've used APIs that provide vertical coordinates first, you'll get it wrong.

You can assign part from a structure and part from floats as well:

CGRect a = (CGRect){
    .origin = myOrigin,
    .size.width = c,
    .size.height = d
};

The CGRectMake function predates C99. I have no evidence to this effect, but I think if C99 had come first CGRectMake probably wouldn't exist at all; it's the sort of crusty function you write when your language has no direct way to perform the initialization. But now it does.

Basically, if you use it for a while, you'll probably come to prefer C99 syntax. It's more explicit, more flexible, more Objective-C-like and harder to screw up.

Unfortunately, as of 4.6 Xcode will not autocomplete structure field names when in the C99 field initializer list.

Sasser answered 6/2, 2013 at 7:55 Comment(8)
thanks. I can see the benefits you describe though XCode autoComplete doesn't seem to know that syntax as yet.Featurelength
I'll have to take your word for it; autocomplete not working is still unsurprising to me. I haven't tried to figure out what specific things break it. :)Sasser
Looks like you're right. I will make a note of this in the answer.Sasser
BTW, you can also do this: CGRect a = (CGRect){ myOrigin, computedSize };Yul
I didn't know that. In cases where the variables are well named, that'd be pretty handy. Thanks.Sasser
I find this to be less headache: self.frame = (CGRect){.origin = CGPointMake(0, newOrigin) }; than: self.frame = CGRectMake(0, newOrigin, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))Dendriform
You are discouraged to use this style by Apple itself. Here, read the introduction to CGRect from Apple: Apple CGGeometry ReferenceGarrot
I don't think that follows. Apple tells you in that intro what's "unsafe": They haven't gone through CGRectStandardize(). Not a big deal for construction (since the CGRect is not an input), or even other things if you feed them through CGRectStandardize() yourself at some point.Sasser
S
4

This is not just shorthand syntax but is also useful when you want to change only the size and not the origin in CGRect and vice versa.

Eg : I want to change only the size and the position has a complicated syntax and I dont want to change it. Noramlly, I would do

CGRect imageRect = CGRectMake(sprite.origin.x,sprite.origin.y,40, 60);

With the other syntax i would do

CGRect imageRect = (CGRect){.size = sprite.size};

also we can directy use add, subtract and multiply methods eg.

 CGRect imageRect = (CGRect){.size = ccpAdd(sprite.size,addsize)};

Hope this helps

Sig answered 6/2, 2013 at 7:44 Comment(0)
S
0

it looks like C99 / GCC style initializing http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

Sagesagebrush answered 6/2, 2013 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.