Xcode Code Completion Macros
In the previous post I described the basics for working with code completion in Xcode. In this post I will show how you can use built-in text macros to insert various code fragments.
As an example, begin by entering ifelse into Xcode and follow this by pressing Control . (control period) and you’ll see the following code block inserted:
At this point you can add code into the placeholder fields, moving from one field to the next using Control / (control forward-slash).
If you enter a code snippet in which there is more than one possible match, you can cycle through the options by pressing F5 which will bring up a list of options in a popup window, or pressing Command . will cycle through the choices inline in your code.
For example, if you enter if and follow this by pressing F5, you will be shown the window below:
List of Available Macros
The Xcode documentation includes a list of macros available for C, C++ and Objective-C. Some of the macros I use on a regular basis are shown below:
- nss -> NSString
- nsms -> NSMutableString
- nsd -> NSDictionary
- nsmd -> NSMutableDictionary
- a -> array (press Control-. to toggle through options)
- log -> NSLog
- if -> if else (press Control-. to toggle through options)
Text Macros from the Menu System
Xcode includes a menu of text macros for C, C++, HTML among others. You can find these macros in the Edit menu under Insert Text Macro.