How to create a new class in Xcode 4

2012-10-16 12:36:04 UTC

  1. Choose which group you want to put the class into. You must do this first, before anything else, or you will have even more work to do later.
  2. Right-click on the group or on any file reference inside it. You must create the class this way, or you will have even more work to do later.
  3. Choose “New File”. You use this same command to create classes, nibs, storyboards, plists, and files of several other types. There is no “New Class” command, and the command is called “New File” even though it often creates multiple files.
  4. Choose which platform you want to create this class for. You must choose exactly one, even if your project is cross-platform, and even if this class will be cross-platform (e.g., NSManagedObject subclass). Even if your project is single-platform, the platform for which the file(s) should be created will not be inferred from the project’s platform.
  5. (Optional) Choose which group of file templates to look in.
  6. If you performed step 5, and guessed wrong, correct yourself. (For example, OS X nibs are not among the “Resource” templates, even though they go in the Resources subfolder. You want “User Interface”.)
  7. Choose which template to use.
  8. Set the class name.
  9. Set the superclass name.
  10. Turn on “With XIB for user interface” if you’re creating a window controller or view controller.
  11. Choose where to save the file(s).
  12. Nibs created along with a WC or VC are created unlocalized (outside of a .lproj folder), so if you did step 10, select the nib and click “Make localized”.

It seems to me that there is room here for optimization.

3 Responses to “How to create a new class in Xcode 4”

  1. leeg Says:

    11a. Even if your group is associated with a folder inside another group’s folder, Xcode will put the file(s) in the folder for the top-level group. You probably just gave yourself a lot more work to do.

  2. Cris Bennett Says:

    1. Open your project in AppCode
    2. In a unit test or other client of the future class, type the class name
    3. Type Alt-enter, then enter
    4. Fill in class name, group and target

    That’s it.

  3. neil Says:

    I just noticed, you can drag an “Objective-C class” from the “File Template Library” in the Utilities pane (bottom right) directly into the Project Navigator, give it a name, then hit save.

Leave a Reply

Do not delete the second sentence.