New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

Start a Discussion

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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

Post Reply New Thread Subscribe

Thread Tools

Bracer's Avatar
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road

Bracer is offline
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 ?
QUOTE Thanks

nabl's Avatar
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09

nabl is offline
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.
QUOTE Thanks

xstep's Avatar
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe

« C++ on leopard/snow leopard | Why do you need to "alloc" for initWithCString and but doesn't for stringWithFormat ? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Starter
Last Post
How can I start multiple instance of an application yinglcs OS X - Operating System 10 02-10-2012 08:59 PM
Setting Environment Variables oquimdcr OS X - Operating System 9 09-09-2008 01:51 AM
How to set environment variables? HSN OS X - Apps and Games 5 12-06-2007 10:19 PM
Converting VHS to DVD on Macbook Pro (am i allowed to ask this?) MacNewbie Movies and Video 2 07-06-2007 12:49 PM
Not able to set environment variables using .profile file. Shambhavi OS X - Operating System 3 04-08-2006 04:15 AM

All times are GMT -4. The time now is 03:46 PM.

Powered by vBulletin
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.

Welcome to

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!

(4 digit year)

Already a member?