Note that there are some explanatory texts on larger screens.

plurals
  1. POSetter for NSString, how is it working?
    text
    copied!<p>This question follows on from a previous question, that has raised a further issue. What I am trying to understand is just when the pointers and object in this example are being created and what ultimately happens to them. I am still trying to get my head round this, so please excuse any false assumptions I may have made.</p> <pre><code>// MAIN int main (int argc, const char * argv[]) { PlanetClass *newPlanet_01 = [[PlanetClass alloc] init]; [newPlanet_01 setGeekName:@"StarWars"]; } </code></pre> <p>.</p> <pre><code>// CLASS @interface PlanetClass : NSObject { NSString *geekName; } - (NSString*) geekName; - (void) setGeekName:(NSString*)gName; @end </code></pre> <p>.</p> <pre><code>// SETTER - (void)setGeekName:(NSString *)gName { if (geekName != gName) { [geekName release]; geekName = [gName copy]; } } </code></pre> <p>(A) ... When an instance of PlanetClass "newPlanet_01" is first created is the NSString instance variable object created, or just a pointer to a possible future object? If it is just a pointer what am I releasing later in the setter as its just a pointer, not a pointer to an object?</p> <p>(B) ... In the above example "gName" is a pointer to the NSString object @"StarWars"?</p> <p>(C) ... Next is the pointer geekName different from the gName (i.e. if geekName does not already point at @"StarWars")</p> <p>(D) ... geekName release, what is released the first time the code is run, my understanding was that geekName is just a pointer to does not point to anything. Or does release just not release anything this first time?</p> <p>(E) ... Finally geekName = [gName copy]; the newly released geekName is now assigned to point to a <em>copy</em> of gName, what happens to the original gName?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload