Naming Convention - careful!


#1

Be very careful with the naming throughout this chapter with allItems array and allItems method!

In the chapter the author uses

{
    NSMutableArray *allItems;
}
- (NSArray *)allItems;
[code][/code]
---------
---------
- (NSArray *)allItems
{
    return allItems;
}

This can get very confusing for some people (myself) because there really is no way to distinguish if you are talking about an NSMutableArray or a Method.

- (NSArray *)allItems
{
    return [self allItems];
}

This will result in an endless loop because you are calling the allItems method not the Array!

Question: is there any way to use properties with arrays? (so you can use [self allItems])


#2

The syntax will tell you whether you are dealing with a method or with something else:

...
... [self allItems] ... // allItems is a method
... [foo allItems] ...  // allItems is a method

- (NSArray *)allItems  // allItems is a method
{
   return allItems; // All items is not a method; it is an instance variable or a global variable
}

You can always name the instance variables by prefixing them with a single underscore character ‘_’ such as _allItems or by giving them a name different from the method name such as allItemsArray.


#3

Yes, prefixing variable names with an underscore is a way of doing this.

But, it should be obvious that the allItems variable and allItems method are different. Consider this:

- (void)bob 
{

}

- (void)fred
{
     int a = bob;
}

That is an error, because you can’t use the name of a method in the same way you can use a variable.