Date
Dec. 31st, 2024
 
2024年 12月 1日

Post: Renaming Variables in Xcode

Renaming Variables in Xcode

Published 12:12 Dec 09, 2013.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

For both persons and programming variables, a good name is an invitation to a prosperous life. Luckily for your Objective-C variables, changing their name is easier than the legal trouble of changing your own name to 'Max Power'.

Find out how:

'Edit All In Scope'

To rename a symbol present solely inside a function, use the 'Edit All In Scope' function. As it the name implies, this will edit the symbol's name inside the current scope.

To make use of this feature:

  1. Place the cursor on top of the symbol to be renamed and press CTRL+E.
  2. Edit your symbol's name.
  3. Press Enter to finish the edit.

Alternatively, you can try placing your mouse on top of a symbol, wait until it is underlined, select the menu 'Edit All In Scope' and go on from there.

Personally, I found this approach annoying.

Xcode's Refactor

If the symbol you intend to rename is a property of a class, a constant used across your project or any other symbol used in any scope wider than a function or method, you should use Xcode's Refactor instead:

  1. Left-click the symbol to be renamed.
  2. Select Refactor -> Rename from the contextual menu.
  3. Finally, give that poor variable a name it can be proud of.

    If you selected the 'Rename related files' option, Xcode will look for other files using the variable and rename it there but, this method is not fail safe -- keep reading!

Avoid Breaking Stuff: NSStringFromSelector

  1. The first strategy is not to trust Xcode's refactor by a 100% and run a project-wide search for the soon-to-be-replaced identifier. Look particularly for occurrences inside strings passed to NSPredicates or used in KVO. It's pretty easy to miss those ones just to find them later as unexpected NSUndefinedKeyExceptions. Not good...

  2. To help Xcode's refactor tools, use the function NSStringFromSelector() every time you make reference to an object's property (instead of simply typing it inside a string)

    self.name = [aDecoder decodeObjectForKey:@"name"];

    NSStringFromSelector gives Xcode a little bit of context on what that particular string means and a hint to include that occurrence in case of refactoring.

    self.name = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(name))];

Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询