I'm trying to get a better understanding of good Objective-C/Swift practices, and I am currently reading this: https://github.com/facebook/pop/blob/master/pop/POPAnimatableProperty.mm , from the Awesome-iOS repo in GitHub. I don't understand these two bits of code.
1/
static POPStaticAnimatablePropertyState _staticStates[] =
{
/* CALayer */
{kPOPLayerBackgroundColor,
^(CALayer *obj, CGFloat values[]) {
POPCGColorGetRGBAComponents(obj.backgroundColor, values);
},
^(CALayer *obj, const CGFloat values[]) {
CGColorRef color = POPCGColorRGBACreate(values);
[obj setBackgroundColor:color];
CGColorRelease(color);
},
kPOPThresholdColor
},
{kPOPLayerBounds,
^(CALayer *obj, CGFloat values[]) {
values_from_rect(values, [obj bounds]);
},
^(CALayer *obj, const CGFloat values[]) {
[obj setBounds:values_to_rect(values)];
},
kPOPThresholdPoint
},
...
}
I get the block part, that is:
^(CALayer *obj, CGFloat values[]) {
POPCGColorGetRGBAComponents(obj.backgroundColor, values);
}
What I don't get is the two curly braces right after
static POPStaticAnimatablePropertyState _staticStates[] =
What is it supposed to be?
2/Similar, but not identical, this piece of code:
static NSUInteger staticIndexWithName(NSString *aName)
{
NSUInteger idx = 0;
while (idx < POP_ARRAY_COUNT(_staticStates)) {
if ([_staticStates[idx].name isEqualToString:aName])
return idx;
idx++;
}
return NSNotFound;
}
It seems to be a block of code after a variable declaration. Is it supposed to execute everytime the variable is used?
First one is declaring an array of POPStaticAnimatablePropertyState
statically. It seems that POPStaticAnimatablePropertyState
is a struct
itself (possibly via typedef
) which has a number, a block, another block, and another number.
Second one is just a static C function, nothing special there.
This code is not a good Objective-C/Swift practice. It's just regular plain C, possibly written to be easily ported or written in this way for pure performance.