Uiscrollview centre image weird code

Discussion in 'iOS Programming' started by nashyo, Mar 23, 2012.

  1. nashyo macrumors 6502


    Oct 1, 2010
    Ive just found this code on Ray Wenderlich.
    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?
    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?


    Source: http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
  2. chown33 macrumors 604

    Aug 9, 2009
    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:
    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:

    Also try some google searches for C designated initializer.
  3. PhoneyDeveloper macrumors 68030


    Sep 2, 2008
    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

    whatever.frame = (CGRect){ 0, 0, image.size.width, image.size.height };
    which is the C89 style of struct initialization

    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.
  4. nashyo thread starter macrumors 6502


    Oct 1, 2010
    Thanks for the feedback. The way I understand it now is (and please correct me if I'm wrong)

    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'.
  5. chown33 macrumors 604

    Aug 9, 2009
    You're right. That was a mistake. I became disoriented and wandered into a maze of dotty little passages all alike.

Share This Page