May 6, 2010

XCode Rename Refactoring – Use at your own risk.

Having used C# with Visual Studio and ReSharper I tend to take bullet-proof refactorings for granted. I’m just starting down the path of iPhone development. I recently used the rename refactoring in XCode and it appears to work correctly. There’s only one little problem. You can assign methods as the target action for buttons in code using the @selector compiler directive. An example might look like this:

[new_button addTarget:self action:@selector(MyButtonHandler:) forControlEvents:UIControlEventTouchUpInside];

Now go ahead and rename MyButtonHandler to ButtonHandler, go ahead I dare you. The problem is the method will get renamed in both the .m and .h files and anywhere it is called outside of  the @selector. For some reason the calls from @selector will not get renamed. Furthermore, you won’t get a compiler error, you won’t even get a runtime error, you get silent but deadly behavior. I’m guessing the @selector now invokes nil which is not an error, just nothing happens.

I need to get more comfortable with the XCode environment and Objective-C in particular but at a minimum there should be some kind of warning when a refactoring is likely the change behavior in your application. Especially something as counter-intuitive as this. I’m told that @selector can take strings and can resolve actions at runtime so there are times when advanced uses may have to be hand-tweaked. The simple cases though should operate correctly.

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.