I come from an AS3 background. In AS3 when you assign an object to a var you do not copy the object to the var, the var becomes a reference to the object.
var myObject = new MyClass();
myObject.name = "Bananas";
var myRef = {};
myRef.ref = myObject;
trace(myRef.ref.name); // Bananas
myObject.name = "Mango";
trace(myRef.ref.name); // Mango
myObject = null;
trace(myRef.ref); // null
In Objective C, AFAIK, when you assign some object to some property of another object the “object is copied. My confusion comes from the fact that after using the temp object you can release it.
NSMutableString* myString = [[NSMutableString alloc] initWithString:@"Hello"];
[myLabel setText:myString];
[myString release];
… if you can release it means you do not need it anymore. So it is copied? or the “text” property in myLabel is a reference to myString?
As you can guess I’m just starting out in the world of Objective C.
TIA
No. Objective-C also uses references (that’s why you see
*after every class name, it’s actually a pointer). However, Obj-C also has a system of memory management. When you domyString = [[NSMutableString alloc] ...]you are creating a new string which you “own”. But when you call[myLabel setText:myString], the label will retain the string and effectively take ownership. Thus when you call[myString release], you are relinquishing ownership, but because the label still has ownership the string will not actually be deallocated from memory.I encourage you to read the Memory management programming guide for more information.