4

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.

3
  • you could check if UIViewController implements it and if so call the super
    – DanZimm
    Nov 25, 2012 at 17:12
  • 1
    I'm concerning mainly about name clash... Even the method has same name, it's function can be completely different.
    – eonil
    Nov 25, 2012 at 17:14
  • ah gotcha, i was assuming the methods would do the same thing
    – DanZimm
    Nov 25, 2012 at 23:18

2 Answers 2

6

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.

2
  • 3
    (+1) See also Private Methods in the "Coding Guidelines for Cocoa" for Apple's advice when to use a prefix.
    – Martin R
    Nov 25, 2012 at 17:23
  • 1
    I really want to avoid typing of extra letters and ugly underlines, but my opinion is beauty without correctness is critical flaw to a program. Anyway, it looks like there's nothing can protect me from this kind of future name clash.
    – eonil
    Nov 25, 2012 at 17:59
1

Eternal vigilance against naming conflicts is necessary when dealing with late-binding systems such as Obj-C.

Unfortunately this means that you're always at risk for this kind of problem. In this case you did nothing wrong back in the 4.x days. All you did was write a good name for a good idea! But Apple likes that name too, and their stuff will take precedence every time. Your best bet is to add a prefix to your class and method names, especially when extending Apple's stuff.

In this case, just rename your method. The good news is that, in the mobile world, you don't need to patch old app versions while maintaining new app versions. It's only legal to have a single version of your app on the market at any time. What this means is that you can make a permanent change to your code base without having to worry about keeping the change in effect to v5.1.6 of your app, but not for v6.0.2. When a user updates, they always update to the latest version.

You will build experience the hard way (you're doing it now), as I did when I had a very important method called description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.