How to Clean and Restructure Code for iOS Apps

By Rajiv Ramnath

You can use the refactoring tool in Xcode to clean and restructure your iOS development applications. Using the refactoring tool will allow you to achieve the following goals with minimum effort.

  • Rename a symbol (such as a variable or a method) to better reflect what the method does.

  • Take inline code and make it a method.

  • Create a superclass (a skeleton of one, at any rate).

  • Move methods from a class up to a superclass or from a superclass down to a subclass.

The refactoring process is straightforward, which will become clear if you take the following steps in which a method name is changed:

  1. In the Source editor, select a piece of source code that you want to refactor — in this case, getBitmapForSymbol.

  2. Rename it getBitmap because it’s a method of the Symbol class.

  3. Choose the appropriate refactoring command. In this example, use Edit→Refactor→Rename.

    Xcode prompts you for the new name.

    image0.jpg

  4. Enter the new name.

  5. Click Preview.

    A preview of the changes appears.

    image1.jpg

  6. Deselect changes you don’t want to make and click Save.

    The first time you try to do any refactoring, you’ll be prompted to enable (or disable) Automatic Snapshots. We recommend that you enable snapshots, at least the first few times that you refactor code.