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

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Ive just found this code on Ray Wenderlich.
Code:
UIImage *image = [UIImage imageNamed:@"photo1.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};

What's going on with this line of code?
Code:
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}

.origin is a property I assume? But a property of what? And it's written in curly brackets, like a block, but seems to be casted as CGRect. I'm so confused, this has blown me out of the water.

What exactly is this code assigning self.imageview.frame as?

Thanks

Source: http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
 
First, CGRect is the typedef name for a struct type. CGRect is not a class, so the .frame member is not a property.

The { } notation is the initialized-struct notation for an unnamed struct. The unnamed struct is then assigned to the .frame member. Since the .frame member is a struct, the { } notation is permitted in struct assignments.

The {.origin=something, .size=otherthing} is a designated initializer. That is, within the { } initializer you designate the struct member names to be initialized. The classical struct initializer requires an in-order listing of just initializer values; you can't provide member names, so you have to know the exact order of members.

So the whole thing is roughly equivalent to:
Code:
CGRect r;  // struct definition (r is a CGRect struct)
r.origin = CGPointMake(0.0f, 0.0f);  // struct assignment of a CGPoint to r.origin
r.size = image.size;  // struct assignment of image.size to r.size
self.imageView.frame = r;  // struct assignment of r imageView.frame
In the last line, imageView is a property of self, but .frame is a struct member of that object.



Designated initializers were added in C99. See this wikipedia article:
http://en.wikipedia.org/wiki/Struct_(C_programming_language)#Struct_initialization

Also try some google searches for C designated initializer.
 
Not sure why you say that .frame is not a property of UIImageView and .frame is a struct member of UIImageView. frame is obviously a property of UIView and also is not a member struct of UIView.

But that isn't the interesting part of this code.

I would say it's more like this

Code:
whatever.frame = (CGRect){ 0, 0, image.size.width, image.size.height };

which is the C89 style of struct initialization
or

Code:
whatever.frame = CGRectMake(0, 0, image.size.width, image.size.height);

which requires no typecasts and is the usual way this is done.

I guess Wenderlich is using this notation to save a little space, or something.
 
Thanks for the feedback. The way I understand it now is (and please correct me if I'm wrong)

Code:
self.imageView.frame = (CGRect) {.origin = CGPointMake(0.0f, 0.0f), .size=image.size};

.origin and .size are already existing members of the struct '//self.imageView//.frame'.
 
Not sure why you say that .frame is not a property of UIImageView and .frame is a struct member of UIImageView. frame is obviously a property of UIView and also is not a member struct of UIView.
You're right. That was a mistake. I became disoriented and wandered into a maze of dotty little passages all alike.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.