Variables in Objective-C

Objective-C also uses variables for storing data in memory like other programming languages. The variables are defined based on the type data type that will be stored. The general rule followed for naming variable are

  1. Use the camel case convention for variable i.e the first word in lowercase and second word in uppercase. For example if you are planning to store name of station in a variable then you can name it as “stationName”.
  2. You can also find out datatype of variable and name accordingly. For example, “strName” can tell the variable is of string data type and holds a name.
  3. Variables must contain only letters and numbers also it is better to avoid using underscore character.
  4. Be descriptive and avoid single letter or confusing words.

The variables are declared by first defining the datatype, followed by description of variable.

Declaring primitive datatypes

Examples :-

int count = 1;

float width = 23.34;

Declaring Objects

NSString *description;

NSArray *months;

The object variables are preceded with * before the description of variable.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.