5

i follow tutorial and find this code:

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

Its pretty clear What it does, but I'm not understand syntax of this line of code. First time i see something like this: .size = image.size. In dot syntax i expect to see something in front of dot, like self.view, but what is meaning of .size?

Second question is - why there is round brackets and after them curly brackets? I never seen structure like that (){}; before.

My question may sound silly, but now I'm a bit confused, can someone provide explanation? Thank you.

Lanorkin
  • 7,310
  • 2
  • 42
  • 60
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 1
    I've never seen this either but FYI - `{}` are not square brackets, those are "curly braces". Square brackets are `[]` and the `()` are parentheses, not "round brackets". – rmaddy Apr 29 '14 at 18:25
  • 1
    This is syntax from C structs: http://en.wikipedia.org/wiki/C_syntax#Designated_initializers – dasdom Apr 29 '14 at 18:28
  • Thank you dasdom, and thank you rmaddy, that was mistyping :) – Evgeniy Kleban Apr 29 '14 at 18:32
  • 2
    Or you could simply do: `self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);`. – rmaddy Apr 29 '14 at 18:34
  • 1
    Thank you rmaddy, that is the way i use to do such things, for me it much more clearly for understand – Evgeniy Kleban Apr 29 '14 at 18:38

1 Answers1

5

This is the Designated Initializer syntax of C structs. The parentheses () are used to cast the struct to a CGRect. As Martin R points out, the cast is not necessary unless you use compound literal syntax, where you don’t name the parameters.

Community
  • 1
  • 1
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • 3
    `CGRect frame = { .origin=..., .size=... }` would be a designated initializer (where you don't need the cast). `self.imageView.frame = (CGRect){ ... }` is an assigment where the rhs is a *compound literal* (and the cast is required). – Martin R Apr 29 '14 at 18:34
  • Ah, thank you for the clarification. I hope I interpreted it correctly in my edit. – Zev Eisenberg Apr 29 '14 at 18:35
  • 3
    @MartinR: Unless I'm very mistaken, the `{ .origin=… }` is the designated initializer syntax, and the `(CGRect){}` is the compound literal syntax. So this is both. – Chuck Apr 29 '14 at 18:42