• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re Re: String variables in classes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re Re: String variables in classes


  • Subject: Re Re: String variables in classes
  • From: Brian Durocher <email@hidden>
  • Date: Sun, 02 Jan 2011 18:17:19 -0500

Thank you guys for your assistance. I come from a C++ background and
have done embedded design in ASM. But this way of working and
particularly the framework is very new to me. I understand what you are
saying, but the literature I am reading is very disappointingly. I have
solved the issue with the strings soon after I wrote the message but
just for completeness and for the sake of a decent critique I will post
the new code. I love the term you are flailing around here, thats
exactly how I felt until I discovered where I was going wrong. Thanks
Graham and Stephen for your replies and comments.

Graham I believe you asked what does "//does not work" mean. I think the
compiler was giving me a function returns void and should be handled as
such. Basically, function returns nothing.

This line of code seems to work for exactly what I was looking for:

    temp = [[temp stringByAppendingFormat:@" %@ %@ \n", input1, input2]
retain];

although I have to admit I am unsure really what the "retain" will
actually do here with respect to memory.  The the preceding [temp
autorelease] create a memory leak or is this the correct way to handle
memory management within the framework?

-----------------------------------

<code>
#import <Foundation/Foundation.h>

@interface Calculator : NSObject
{
    double accumulator;
    NSString* caption;
    NSString* name;
    NSString* temp;
}

- (id) init;
- (void) setAccumulator:(double) n;
- (void) printAccumulator;

- (NSString*) caption;
- (NSString*) name;
- (NSString*) temp;

- (void) setCaption: (NSString*)input;
- (void) setName: (NSString*)input;
- (void) setTempString: (NSString*)input;
- (void) combineString: (NSString*) input1 : (NSString*) input2;

- (void) printCaption;
- (void) printName;
- (void) printTemp;


@end

@implementation Calculator

- (id) init
{
    if ( self = [super init] )
    {
        [self setAccumulator:0];
        [self setCaption:@"Default Caption"];
        [self setName:@"Default Name"];
        [self setTempString:@"empty"];
    }
    return self;
}

- (void) setAccumulator: (double) n
{
    accumulator = n;
}

- (void) printAccumulator
{
    NSLog(@"Accumulator = %f", accumulator);
}

- (NSString*) caption {
    return caption;
}

- (NSString*) name {
    return name;
}

- (NSString*) temp {
    return temp;
}

- (void) setCaption: (NSString*)input
{
    [caption autorelease];
    caption = [input retain];
}

- (void) setName: (NSString*)input
{
    [name autorelease];
    name = [input retain];
}

- (void) setTempString: (NSString*)input
{
    [temp autorelease];
    temp = [input retain];
}

- (void) combineString: (NSString*) input1 : (NSString*) input2
{
    [temp autorelease];
    temp = @"";
    temp = [[temp stringByAppendingFormat:@" %@ %@ \n", input1, input2]
retain];
}


- (void) printCaption
{
    NSLog(@"%@ \n", self.caption);
}
- (void) printName;
{
    NSLog(@"%@ \n", self.name);
}
- (void) printTemp;
{
    NSLog(@"%@ \n", self.temp);
}

@end


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...

    Calculator *theDevice = [[Calculator alloc] init];

    [theDevice init];
    [theDevice printAccumulator];
    [theDevice setAccumulator:10];
    [theDevice printAccumulator];
    [theDevice setName: @"Brian D"];
    [theDevice setCaption: @"Rocks"];

    [theDevice combineString:[theDevice name] : [theDevice caption]];

    [theDevice printTemp];
    [theDevice printName];
    [theDevice printCaption];


    NSLog(@"The two variables: %@, %@ \n", [theDevice name], [theDevice
caption]);
    NSLog(@"Hello, World!");
    [theDevice release];

    [pool drain];
    return 0;
}

</code>


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Re Re: String variables in classes
      • From: Graham Cox <email@hidden>
    • Re: Re Re: String variables in classes
      • From: "Stephen J. Butler" <email@hidden>
    • Re: Re Re: String variables in classes
      • From: Charles Srstka <email@hidden>
    • Re: Re Re: String variables in classes
      • From: Charles Srstka <email@hidden>
  • Prev by Date: Re: iPhone UISearchDisplayController how to handle memory warning
  • Next by Date: Re: Re Re: String variables in classes
  • Previous by thread: Re: Funky icons in UITabBar
  • Next by thread: Re: Re Re: String variables in classes
  • Index(es):
    • Date
    • Thread