Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well there are few problems in your code, but basic problem is you need NSMutableString string, not NSString to make your code work.</p> <ol> <li><p>in init method, correct code to initialize is,</p> <p><code>someCommonString = [[NSMutableString alloc] initWithString:@"one "];</code></p></li> <li><p>You have to return the object (self) from init, otherwise it will not work, like this.</p> <p><code>return self;</code></p></li> <li><p>If you wanted to append the string, it should be NSMutableString, not NSString.</p> <p><code>[[self someCommonString] appendString:test1];</code></p> <p><code>[[self someCommonString] appendString:test2];</code></p> <p><code>[[self someCommonString] appendString:test3];</code></p></li> <li><p>In dealloc method, you call dealloc method of super, not release the super. So correct it it like this.</p> <p><code>[super dealloc];</code></p></li> <li><p>There is no need to release <code>test1</code> and <code>test3</code>, because they are <strong>autorelease</strong>.</p></li> </ol> <p>I have witten the correct code, try this out.</p> <pre><code>@interface myClass : NSObject { @private NSMutableString* someCommonString; } @property (retain, nonatomic) NSMutableString* someCommonString; - (void) doTheJob; @end @implementation myClass @synthesize someCommonString; - (id) init { self = [super init]; if(self) { someCommonString = [[NSMutableString alloc] initWithString:@"one "]; } return self; } - (NSString*) appendString:(NSString*) stringToAdd { NSString* result = [someCommonString stringByAppendingString: stringToAdd]; return result; } - (void) doTheJob { NSString* test1 = @"two "; NSString* test2 = [[NSString alloc] initWithString: @"three "]; NSString* test3 = [NSString stringWithFormat:@"four "]; [[self someCommonString] appendString:test1]; [[self someCommonString] appendString:test2]; [[self someCommonString] appendString:test3]; NSLog(@"%@", someCommonString); [test2 release]; } - (void) dealloc { [someCommonString release]; [super dealloc]; } @end </code></pre>
 

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