Prevent Erroneous Property Assignment
Posted
by
Gordon
on Stack Overflow
See other posts from Stack Overflow
or by Gordon
Published on 2011-01-16T19:36:34Z
Indexed on
2011/01/16
19:53 UTC
Read the original article
Hit count: 175
Porting android applications to iphone applications always gives me the following pattern that I accidentally create:
- (void) myFunc:(id)prop {
self.property = property;
}
Which instead should be:
- (void) myFunc:(id)prop {
self.property = prop;
}
This always causes my program to quietly break because property gets reset to its existing value rather than being set to the new value, 'prop'. I cannot name the parameter 'prop' to 'property' since the compile complains that the parameter masks the instance variables visibility.
Is there a good way to avoid this situation? There are no compiler warnings. Is there a way to make xcode prevent this? I cannot see very many situations where you would set a property to the value of its underlying instance variable (maybe to trigger a KVO binding?), but I don't see myself doing that in majority of cases.
I understand the above code is synthetic and should be done with @synthesize, but I am just using it as a simplified example to illustrate my point.
© Stack Overflow or respective owner