Note that there are some explanatory texts on larger screens.

plurals
  1. POAllocating memory for NSString?
    primarykey
    data
    text
    <p>I am new to <a href="http://en.wikipedia.org/wiki/Objective-C" rel="nofollow noreferrer">Objective-C</a> and I am a little curious about how I should be managing the memory for the local NSString variables shown below and the associated instance variables inside the class object. The code I have works fine, but just curious as to best practice.</p> <p>Edited to include full code, nothing special, like I say I am just curious if in this context I should be doing alloc/release on the NSString objects.</p> <pre><code>// MAIN ------------------------------------------------------------------- ** #import &lt;Foundation/Foundation.h&gt; #import "PlanetClass.h"; int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *planet_01_Geek; NSString *planet_02_Geek; // Create planets PlanetClass *newPlanet_01 = [[PlanetClass alloc] init]; [newPlanet_01 setGeekName:@"StarWars"]; PlanetClass *newPlanet_02 = [[PlanetClass alloc] init]; [newPlanet_02 setGeekName:@"Dune"]; // Query a planet planet_01_Geek = [newPlanet_01 geekName]; planet_02_Geek = [newPlanet_02 geekName]; // Print details NSLog(@"Planet Geek = %@", planet_01_Geek); NSLog(@"Planet Geek = %@", planet_02_Geek); // Clean up [newPlanet_01 release]; [newPlanet_02 release]; [pool drain]; return 0; } </code></pre> <p>..</p> <pre><code>// CLASS HEADER ----------------------------------------------------------- ** #import &lt;Foundation/Foundation.h&gt; @interface PlanetClass : NSObject { NSString *geekName; } - (NSString*) geekName; - (void) setGeekName:(NSString*)gName; @end // ------------------------------------------------------------------------ ** </code></pre> <p>..</p> <pre><code>// CLASS BODY ------------------------------------------------------------- ** #import "PlanetClass.h" @implementation PlanetClass - (NSString*)geekName { return geekName; } - (void)setGeekName:(NSString*)gName { geekName = gName; } @end // ------------------------------------------------------------------------ ** </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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