Sunday, June 2, 2013

NSMutableString appendString example in Objective C (iOS).


NSMutableString appendString

Adds to the end of the receiver the characters of a given string.

- (void)appendString:(NSString *)aString

Parameters of [NSMutableString appendString]
aString
The string to append to the receiver. aString must not be nil

NSMutableString appendString example.
- (NSString *)readArray
{
    NSMutableString *output = [[NSMutableString alloc] init];
    int i;
    int arraySize = widget.children.count;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    for (i = 0; i < arraySize; i++)
    {
     [output appendString:[widget.children objectAtIndex:i]];
    }
    [pool release];
    return output;

}

Example of [NSMutableString appendString].
NSMutableString *teststring = [[NSMutableString alloc] init];
[teststring appendString:@"hey"];
NSLog("%@", teststring);

NSMutableString appendString example.
NSMutableString *buffer = [[NSMutableString alloc] init]; // retain count = 1. Because of the "alloc", you have to call a release later

[buffer appendString:@"abc"];
NSLog(@"1 : %@", buffer);
[buffer appendString:@"def"];
NSLog(@"2 : %@", buffer);

[buffer release]; // retain count = 0 => delete object from memory

End of NSMutableString appendString example article.