Results 1 to 3 of 3
  1. #1

    Bracer's Avatar
    Member Since
    Oct 07, 2009
    Location
    Singapore
    Posts
    14
    Why is it that instance variables with the same name are allowed ?
    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 ?

  2. #2

    nabl's Avatar
    Member Since
    Dec 13, 2007
    Location
    United States of America
    Posts
    256
    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.

  3. #3

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    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.
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to run another instance of an Application by double clicking on its Icon
    By ankitverma05 in forum OS X - Operating System
    Replies: 0
    Last Post: 10-06-2012, 03:13 AM
  2. How to run another instance of an Application by double clicking on its Icon
    By ankitverma05 in forum OS X - Operating System
    Replies: 2
    Last Post: 10-05-2012, 02:11 PM
  3. How to run another instance of an Application by double clicking on its Icon
    By ankitverma05 in forum OS X - Operating System
    Replies: 0
    Last Post: 10-05-2012, 01:20 AM
  4. Opening a second instance of an application
    By CarpentersKeys in forum OS X - Operating System
    Replies: 5
    Last Post: 03-13-2012, 12:32 AM
  5. How can I start multiple instance of an application
    By yinglcs in forum OS X - Operating System
    Replies: 10
    Last Post: 02-10-2012, 09:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •