ios - CGRect syntax I haven't seen before -
i saw syntax below in illustration code , not sure understand it.
cgrect imagerect = (cgrect){.size = baseimage.size};
is shorthand way of initializing cgrect
equivalent to:
cgrect imagerect = cgrectmake(0,0,baseimage.size.width, baseimage.size.height);
is there benefit syntax aside less typing?
that's c99 initializer syntax. can utilize structure.
the main advantage objective-c gives objective-c syntax, fields close values rather implied positioning. (that's not intentionally similar, or it's advantage. nice.)
it's more typing, utilize everywhere now.
consider:
cgrect = cgrectmake(a+c/2, b+d/2, c, d);
in order understand this, need understand order of parameters. need able grab commas eyes. in case, that's pretty easy, if expressions more complicated you'd storing them in temporary variable first.
the c99 way:
cgrect = (cgrect){ .origin.x = a+c/2, .origin.y = b+d/2, .size.width = c, .size.height = d };
it's longer, it's more explicit. it's easy follow assigned what, no matter how long look are. it's more objective-c method. after all, if cgrect class, this:
cgrect *a = [[cgrect alloc] initwithoriginx:x originy:y width:w height:h];
you can things this:
cgrect = (cgrect){ .origin = myorigin, .size = computedsize };
here, you're building rectangle using cgpoint
, cgsize
. compiler understands .origin
expects cgpoint
, , .size
expects cgsize
. you've provided that. all's gravy.
the equivalent code cgrectmake(myorigin.x, myorigin.y, size.width, size.height)
. using cgrectmake
you're no longer expressing same kind of meaning compiler. can't stop assigning part of size origin. won't stop assigning width height. doesn't give clue x , y; if you've used apis provide vertical coordinates first, you'll wrong.
you can assign part construction , part floats well:
cgrect = (cgrect){ .origin = myorigin, .size.width = c, .size.height = d };
the cgrectmake
function predates c99. have no evidence effect, think if c99 had come first cgrectmake
wouldn't exist @ all; it's sort of crusty function write when language has no direct way perform initialization. does.
basically, if utilize while, you'll come prefer c99 syntax. it's more explicit, more flexible, more objective-c-like , harder screw up.
unfortunately, of 4.6 xcode not autocomplete construction field names when in c99 field initializer list.
ios core-graphics
No comments:
Post a Comment