Skip to main content
Added link to Objective-C [Wikipedia]. Minor edit: grammar/spelling/case/punctation/etc.
Source Link
Peter Mortensen
  • 31.2k
  • 22
  • 110
  • 134

I am new to objective-cObjective-C 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.

I am new to objective-c 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.

I am new to Objective-C 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.

deleted 1 characters in body; edited title
Source Link
GEOCHET
  • 21.2k
  • 15
  • 78
  • 99

Allocating memory for NSString  ?

I am new to objective-c 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.

Edited to include full code, nothing special, like I say I am just curious if in this context I should be doing alloc  / releaserelease on the NSString objects.

Allocating memory for NSString  ?

I am new to objective-c and 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.

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.

Allocating memory for NSString?

I am new to objective-c 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.

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.

added 1540 characters in body
Source Link
fuzzygoat
  • 26.3k
  • 48
  • 170
  • 299

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.

// VariablesMAIN ------------------------------------------------------------------- **
#import <Foundation/Foundation.h>
#import "PlanetClass.h";

int main (int argc, const char * argv[]) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *planet_01_Geek;
    NSString *planet_02_Geek;

    // AssignCreate localplanets
 from object  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];

    // OutputPrint localdetails
 to console
  NSLog(@"Planet Geek    = %@", planet_01_Geek);
    NSLog(@"Planet Geek    = %@", planet_02_Geek);

    // Clean up
    [newPlanet_01 release];
    [newPlanet_02 release];
    [pool drain];
    return 0;
}

..

// CLASS HEADER ----------------------------------------------------------- **
#import <Foundation/Foundation.h>

@interface PlanetClass : NSObject {
NSString *geekName;
}

- (NSString*) geekName;
- (void) setGeekName:(NSString*)gName;
@end
// ------------------------------------------------------------------------ **

..

// CLASS BODY ------------------------------------------------------------- **
#import "PlanetClass.h"

@implementation PlanetClass

- (NSString*)geekName {
    return geekName;
}
- (void)setGeekName:(NSString*)gName {
    geekName = gName;
}
@end
// ------------------------------------------------------------------------ **
// Variables
NSString *planet_01_Geek;
NSString *planet_02_Geek;

// Assign local from object
planet_01_Geek = [newPlanet_01 geekName];
planet_02_Geek = [newPlanet_02 geekName];

// Output local to console
NSLog(@"Planet Geek    = %@", planet_01_Geek);
NSLog(@"Planet Geek    = %@", planet_02_Geek);

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.

// MAIN ------------------------------------------------------------------- **
#import <Foundation/Foundation.h>
#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;
}

..

// CLASS HEADER ----------------------------------------------------------- **
#import <Foundation/Foundation.h>

@interface PlanetClass : NSObject {
NSString *geekName;
}

- (NSString*) geekName;
- (void) setGeekName:(NSString*)gName;
@end
// ------------------------------------------------------------------------ **

..

// CLASS BODY ------------------------------------------------------------- **
#import "PlanetClass.h"

@implementation PlanetClass

- (NSString*)geekName {
    return geekName;
}
- (void)setGeekName:(NSString*)gName {
    geekName = gName;
}
@end
// ------------------------------------------------------------------------ **
Removed signoff.
Source Link
Abizern
  • 151.7k
  • 41
  • 209
  • 260
Loading
Retagged: -xcode +memory
Link
Peter Hosey
  • 96.5k
  • 15
  • 214
  • 373
Loading
Source Link
fuzzygoat
  • 26.3k
  • 48
  • 170
  • 299
Loading