Sunday, May 19, 2013

NSString stringByAppendingString example ios


[NSString stringByAppendingString]

Returns a new string made by appending a given string to the receiver.
- (NSString *)stringByAppendingString:(NSString *)aString
Parameters
aString
The string to append to the receiver. This value must not be nil.

Important: Raises an NSInvalidArgumentException if aString is nil.
Return Value
A new string made by appending aString to the receiver.
Discussion of [NSString stringByAppendingString]
This code excerpt, for example:
NSString *errorTag = @"Error: ";
NSString *errorString = @"premature end of file.";
NSString *errorMessage = [errorTag stringByAppendingString: errorString];
produces the string “Error: premature end of file.”.
Example of [NSString stringByAppendingString]
// creates an autoreleased string
fullCmdString = [NSString stringWithString:[inStr stringByAppendingString:add]];  

// also creates an autoreleased string
fullCmdString = [inStr stringByAppendingString:add];

// works great, not an autoreleased string
fullCmdString = @"basic text text added";
Example of [NSString stringByAppendingString]
// OK! Avoid the autorelease pool (at least, for fullCmdString)
fullCmdString = [[NSString alloc] initWithString:[inStr stringByAppendingString:add]]; 

// also OK! It's in the pool, but it's also been retained.
fullCmdString = [[inStr stringByAppendingString:add] retain];

// also OK
fullCmdString = @"basic text text added";
Example of [NSString stringByAppendingString]
+ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSLog(@"stack array :: %@",stack);
NSString *result;
NSString *displayString = [[NSString alloc]init];
id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];

NSLog(@"topOfStack :: %@", (NSString*)topOfStack);

if ([self isNoOperandOperation:topOfStack]) {
    displayString = [[(NSNumber*)displayString stringValue] 
                                stringByAppendingString:(NSString*)topStack];
    result = displayString;
    }
else if ([self isOperation:topOfStack]){
    id nextInStack = [stack lastObject];
    if (topOfStack) [stack removeLastObject];
    displayString = [[(NSNumber*)displayString stringValue] 
                                stringByAppendingString:(NSString*)nextInStack];
    displayString = [[(NSNumber*) displayString stringValue] 
                                stringByAppendingString:(NSString*)topStack];
    result = displayString;