I thought an variable in objective-c is just a reference to an object somewhere in memory. So for my understanding, the result must have been "one", because at the end i assign the object's memory address of str1 to str2, and previously I had assignend the memory adress of str2 to test.
NSString *str1 = [NSString stringWithCString:"one"];
NSString *str2 = [NSString stringWithCString:"two"];
test = str2; // test is an instance variable. I use no getter/setter here! just for testing!
str2 = str1;
NSLog(test); // = "two" ??
-
This is how pointers work. The result you see is normal and correct.
Let's list all of your variables and what strings they point to in memory as they are declared:
str1 -> "One" str2 -> "Two"Then, you execute some assignment instructions:
test = str2;This assigns the value of the pointer
str2totest. So the pointers arestr1 -> "One" str2 -> "Two" test -> "Two"Then
str2 = str1;Assigns the value of the pointer
str1tostr2. Now the pointers arestr1 -> "One" str2 -> "One" test -> "Two"Then you print
test, which is pointing to whatstr2was pointing to originally, which is "Two".I think you think that since you assigned
str2to the value ofstr1, andtestto the value ofstr2, that the value ofstr1somehow cascades intotest. This is not the case. Oncetest's value is assigned, the information regarding where that value came from is lost. If you wanttest's value to be the same asstr1's, you have you reverse the order of your assignment operations:str2 = str1; test = str2;Thanks : Thnaks. I thing i've got that now. So those pointers always return the value, and not the memory adress of the object itself?Welbog : Well, no. The value of the variable is an address, like you think. It's just that there is no "cascade" of assignments.Martin Peck : +1 for clear illustration -
You have this:
test = str2; // test is an instance variable.testing! str2 = str1; NSLog(test); // = "two" ??Now, let's pretend all those variables were ints.
On the first line, variable
testis set to the value of variablestr2. On the second line, variablestr2is set to the value of variablestr1. On the third line, we print out the value oftest, and it's indeed the value thatstr2was when we assignedstr2to test. It really doesn't matter at all that we subsequently assignedstr2a different value.Ok, but the variables are really pointers, you say, not ints.
No matter: think of a pointer as just a number, the address of somewhere in memory. And passing that to NSLog makes NSLog print not the value of the pointer, but the value of what the pointer points to, which is an array of char.
Thanks : Thanks. So it would be right to say, that the value of an variable always is an memory adress? and at the "location" of that adress is the actual value, or object, or whatever?tpdi : No, that's only true for pointers.Thanks : Or another try: Variables always point to an memory adress. Behind an memory adress always is an specific value or object. Is that right?Thanks : Another Q: Every time I work with objects, my variables that represent them are "pointers", right?tpdi : No, only pointers point to a memory address. An int, for example, doesn't. An object variable may be a pointer.Thanks : When I look in XCode debugger, an int var shows up the value in "value", and an object var (pointer) shows something like an memory adress in "value". I think the light bulp is getting brighter a little bit now ;)tpdi : Yes, because the value of a pointer is a memory address. Thus, getting back to your original question, when you assign a pointer to another pointer (test = str2), you're copying the memory address. -
A variable is a container. The assignment takes whatever's on the right side and puts it also in whatever's on the left side. In other words, it copies the value from one variable to the other; in this case, those values are pointers (memory addresses).
So here's what you did:
- Take the address that's in the
str2variable and put the same address in thetestvariable. - Take the address that's in the
str1variable and put the same address in thestr2variable.
Step 2 doesn't affect the
testvariable; your only assignment to that variable was in step 1.Important: You're not copying what's at the address; you're copying the address itself. There's still only one “two” string, but between steps 1 and 2, the address of that object is in both
str2andtest. Then, in step 2, you replace the address instr2with the address of the “one” string, which also exists instr1, leavingtestas the only variable that still holds the address of the “two” string.BTW,
stringWithCString:is deprecated. UsestringWithUTF8String:orstringWithCString:encoding:instead. - Take the address that's in the
0 comments:
Post a Comment