1.7. Declaring Variables in Objective-C
Problem
You want to use variables with clear names in your iOS apps.
Solution
Apple conventions dictate certain rules for variable names. Determine the type of the variable (for instance, integer, array, string, etc.) and then a descriptive name for it. In an empty line of code, place the type of your variable first, following by its name. Variable names are advised to follow these rules:
Follow the camelCase naming convention. If the variable name is one word, all letters must be lowercase. If the variable name is more than one word, the first word must be entirely lowercase and the subsequent words must have their first letter uppercase and the rest of their letters lowercase. For instance, if you want to have a counter variable, you can simply name it
counter. If you want to call your variable “first counter”, declare itfirstCounter. The variable name “my very long variable name” would becomemyVeryLongVariableName. (Names of that length are quite common in iOS programs.)Variables should ideally have no underline in their names. For instance,
my_variable_namecan and perhaps should be changed tomyVariableName.Variable names should contain only letters and numbers (no punctuation such as commas or dashes). This is a restriction in the Objective-C language and many other languages, but this helps keeping variable names tidy.
Let’s have a look at a few examples. There are a few primitive data types in Objective-C. A data type is a name that specifies ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access