Why is it that instance variables with the same name are allowed ?

Joined
Oct 7, 2009
Messages
14
Reaction score
0
Points
1
Location
Singapore
Code:
NSMutableArray* array = [ [NSMutableArray alloc] init ];
for (counter=0 ; counter<10 ; counter++)
{
	NSMutableString* string = [NSMutableString stringWithFormat:@"%d",counter];
	[array addObject:string];
}
NSLog(@"The number of Objects in the Array is %d.",[array count]);
NSLog(@"Index 5 of Array contains %@.",[array objectAtIndex:5]);
[array removeObjectAtIndex:5];
NSLog(@"Index 5 of Array is now %@.",[array objectAtIndex:5]);
[array release];
There are ten Object instances in the Array with the same name !
They are all call string !
Shouldn't Objective C raise some sort of "duplicate member" errors ?
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
NSArrays don't hold objects by name; they only hold pointers. The variable with the name string only exists from its initialization to the end of each run of the for loop; by the time a new object is initialized with that name, the other one is no longer available (although the data that was pointed to by the variable is still there until released). You would only get an error if you tried to define another variable with the name string inside the for loop. Similarly, if you tried to access string after the closing bracket of the for loop without redefining it, you would get an error because the string variable is no longer defined at that point. These occur because of the scope of the variable—or the area of code where a variable can be accessed. For a quick reference on variable scope, this site looks good, though there are other, more-detailed references available if you need them.

Also, note that instance variables (often called ivars) are the variables defined to exist for every instance of a particular class. So for your exact question, the answer is that you can't have two instance variables with the same name. For example, if you had a Triangle class, you could not define two baseWidth instance variables. You could, however, have a baseWidth instance variable for a separate Pentagon class. Then if you had a Triangle object and a Pentagon object in the same function, sending either object the getter baseWidth message would return the proper value. This concept is called polymorphism—the ability of multiple classes to respond to messages with the same name.

I hope I explained that clearly; those concepts can be rather confusing. :Oops:
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
There is one pointer called string to an object id of type NSMutableString. Every time through your loop, you are changing that pointer, then assigning the new pointer to your array. Your array contains 10 pointers after exiting the loop.

Based on the questions you have been asking, I think you need to learn some basic C. This "Learn C for Cocoa" might be good enough. There are many good tutorials at that site.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top