Coding Style TAB
The Coding Style TAB is where you can set preferences for prefixes and suffixes, as well as formatting for properties, methods, pragma marks and init styles. This is also where you define your Configuration Sets that allow you to quickly set ALL Accessorizer settings in one fell swoop, allowing you to keep your coding practices and styles consistent across various projects.
There are several important switches and options.
You can use both a prefix and a suffix simultaneously for your ivars or synthesized variables, if you wish, but this is not common. It’s more common to use one or the other, or not any at all.
There are different schools of thought on whether or not you should use a prefix. If you’ve taken the Stanford iOS course taught by professor Paul Hagerty, formerly of NeXT (his highly recommended course is free on iTunes U), you’ll see that Paul emphasizes time and time again that you should, without fail, prefix your variables with an underscore. In fact, some of Apple’s templates and come with ivars prefixed with an underscore. Whether it’s an underscore or some other prefix, Paul is adamant about this. Additionally, Apple has said they’d like to standardize on the underscore prefix.
Arguments aside, let’s say you subscribe to this coding style and you want an underscore prefix such that your backing variables are not named the same as your accessors methods or properties. You simply enter the underscore (or whatever prefix you wish) into the Ivar Prefix text field and hit RETURN. Click the radio button for the prefix to enable it.
Before we take a look at the “Automatically insert prefix/suffix... “ switch at the top of the panel, let’s just look at the prefix setting as an example and come back to this switch later. Turn the “Automatically insert...” switch of OFF for now.
1) Prefixing ivars in ivar declarations
2) Prefix synthesized variables for properties - “Automatically insert prefix/suffix...”
3)Temporary ivar/property declaration
4) Multiple prefixes
PREFIX
With your Ivar Prefix now set to an underscore and enabled, there are several scenarios for how this can work for you as outlined below.
If you’ve defined an Ivar Prefix in your settings and selected its respective radio button in the radio matrix as you see above, it’s important when you declare your ivars that you declare them with the prefix. If you do, Accessorizer will detect and remember those ivars with their prefixes for subsequent code generation. When creating @property statements for those ivars, the prefix will not be present as expected, but when creating @synthesize statements or other types of code, such as init methods etc., the actual name of the synthesized variable will be used where appropriate.
You can keep your setting for prefix ON, but it will be ignored if you don’t provide the prefix when you declare your ivar. This gives you flexibility. Note QVNote *_note; vs the other declarations.
As you can see from the above, while the ivars are prefixed, the properties are not prefixed, which is what you want. However, as you may know, in the 64-bit runtime, your ivars can be synthesized and you do not need manually declare the ivars in the @interface block. NOTE: some developers continue to declare the ivars for debugging purposes.
Let’s say you are not declaring ivars, and are only declaring properties and synthesizing your ivars... but, as in the case of wanting hidden backing variables, you want your backing variables to be prefixed. This is where the “Automatically insert prefix/suffix for @property(s) ... “ switch comes in.
Normally, you would have to type your @property statements with the @property specifiers, perhaps IBOutlet keywords, the TYPEs and NAMEs of each of your property(s). Tiresome! Laborious! You then have to go to your implementation file, type your @synthesize statements and now include your prefix (or suffix). Further, any other code generation including dealloc, init methods, keyed-archiving, accessor overrides etc, you will have to maintain that consistency of making sure you identify your backing variable with the prefix wherever necessary.
Accessorizer takes care of all that for you. You can select your properties and generate code from those properties. And even though your properties will not include the prefix or suffix, your code generation will include the prefix and/or suffix when necessary.
To do this, you must turn ON the “Automatically insert prefix/suffix ... “ switch at the top of the Coding Style panel like so:
When this switch is on, Accessorizer will automatically insert a prefix and/or suffix for you based on your settings. You would first select your @property statements and invoke the Accessorizer Action Panel service (which sends those properties over to Accessorizer behind the scenes). Then when you generate code, such as @synthesize statements, where appropriate, Accessorizer will insert the prefix or suffix for you.
As shown in the image below, I’m generating Implementation code from @property statements instead of ivar declarations (via cmd-2). NOTE that while the properties do not have the prefix, the @synthesize statement declare to synthesize using the underscore prefix as defined in our prefix setting. Also note -dealloc and -viewDidUnload (ARC is OFF in this example).
IMPORTANT: You do not want to have this switch turned on if you are manually declaring ivars WITHOUT the prefix or suffix in your @interface block as Accessorizer will “automatically insert... “ the prefix or suffix into your @synthesize statements and other code generation.
Your declared ivars would be without the prefix and/or suffix, yet your @synthesize statements would use the prefix and/or suffix. This would be wrong!
NOTE: if the switch is on, and if you declare ivars in the @Interface block with the prefix/suffix defined and enabled in your settings, Accessorizer will recognize the prefix and/or suffix and will not add it a second time to your variables.
KEY FEATURE: A key feature of this switch being ON is that when you are declaring properties, you can simply create “temporary ivar declarations” without having to type the prefix or suffix. Accessorizer will generate the appropriate properties, and when generating @synthesize and other code, will include the prefix and/or suffix where appropriate and necessary. This is an enormous time saver! It is described and demonstrated in more detail below.
Assuming you have gone through the QuickStart pages (3 of them) , the following will be something of a refresher. In this short video clip below, you see a bunch of TYPES and NAMES or “temporary ivar declarations” that will be turned into @property statements. And, with the prefix/suffix settings along with “Automatically insert prefix/suffix” turned ON, the code generated will honor those settings. The properties will not have the prefix/suffix, yet the @synthesize and other code generation will use the prefix/suffix where appropriate.
This video demonstrates the use of temporary ivar declarations converted to property statements.
In some cases, your coding style may require the use of more than one possible prefix! ie: variable prefixes in the sense that any of one or more prefixes may be in use!
For example: If you want to use the “_” prefix for most of your ivars, the “o” prefix for IBOutlets, and the “w” prefix for weak pointers, you could enter all of these in the prefix field separated by a space (the suffix field does not support multiple suffixes).
You can see how Accessorizer appropriately handles the variable set of prefixes. Note, Accessorizer smartly determines the “w” in weather is not a prefix, but is part of the variable’s name.
VERY IMPORTANT: IF you are generating your code off of properties, since properties do not contain prefixes, there’s no way of Accessorizer to interpret multiple prefixes. In this case, Accessorizer will just use the FIRST prefix in the list when generating code such at @synthesize etc. but this is ONLY if “Automatically insert prefix/suffix is turned ON”. If the switch is off, you will not get support for prefixes when generating code from properties. Also, if it is ON, and if you are generating from ivars, Accessorizer will only detect and use the first prefix in the list.