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.