Objective-C supports subclassing and categories which allow you to add methods to an existing class. Here’s my case.
// Made at earlier than iOS 5.x
@interface MyVC : UIViewController
- (void)childViewControllers;
@end
There was not the childViewControllers method before iOS 5.0. I could add the method without any concern. But now the UIViewController has a method with the name. If I build the legacy code written for iOS 4.x, the method will be overridden and it would make unpredictable result.
Is there any technique to defense my code from this kind of problems? This can be happen on categories or (maybe) something else.
One option is to add a prefix to all of your method names. Do this anytime you extend a standard framework class.
This same technique is used for class names to avoid possible collision in the future since there is no namespace like in Java.
Here’s another thought to consider. How often does this happen? You need to weigh the effort of having to deal with the rare name collision by going back and renaming your method and updating affected code versus having to come up with a prefix naming scheme and typing those extra characters over and over every day.