Sunday, June 2, 2013

NSMutableString initWithCapacity example in Objective C (iOS).

NSMutableString initWithCapacity

Returns an NSMutableString object initialized with initial storage for a given number of characters,

- (id)initWithCapacity:(NSUInteger)capacity

The number of characters the string is expected to initially contain.

Return Value
An initialized NSMutableString object with initial storage for capacity characters. The returned object might be different than the original receiver.

Discussion of [NSMutableString initWithCapacity]
The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string.

NSMutableString initWithCapacity example.
NSString *name = @"abcdefghi" ;
    int len = [name length];
    NSMutableString *reverseName = [[NSMutableString alloc] initWithCapacity:len];

    for(int i=len-1;i>=0;i--)
        [reverseName appendFormat:[NSString stringWithFormat:@"%c",[name characterAtIndex:i]]];



Example of [NSMutableString initWithCapacity].
NSMutableString *str1 = [[NSMutableString alloc]initWithCapacity:0];
NSMutableString *str2 = [[NSMutableString alloc]initWithCapacity:0];

for(int pq=1; pq {
    str1 = [NSMutableString stringWithFormat:[documentManager wholeTextForPage:pq]];
    str2 = (NSMutableString *)[str2 stringByAppendingFormat:str1];
    NSLog(@"content of page chumma1:  %@",str1);

NSLog(@"cwhole text:  %@",str2);

NSMutableString initWithCapacity example.
str1 = [[NSMutableString alloc] initWithCapacity:0];
for (int pq=1; pq<=temp; pq++)
  str1 = [str1 stringByAppendingString:[NSString stringWithString:[documentManager wholeTextForPage:pq]]];

End of NSMutableString initWithCapacity example article.