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.
Many people choose to name their instance variables with a leading underscore. This would allow you to name your parameter
property, and it’s unlikely you’d accidentally type an underscore.Or, you could name your parameter
newProperty, which might make it different enough that it’d be harder to confuse?