views:

291

answers:

1

I need to limit the number of characters that can be appended to an nsmutablestring to 10. Also i need to clear its contents on a button press. What is the best method to do that? pls help

+1  A: 

You can write a category. Something like this:

@interface NSMutableString (append10)
-(void)appendString:(NSString *)aString;
@end

@implementation NSMutableString (append10)

-(void)appendString:(NSString *)aString
{
    NSString *toAppend = aString;
    if ([aString length] > 10)
    {
        // truncate string
    }

    [super appendString:toAppend];
}

@end
Carl Norum
or you could have whatever method that builds the string check the length :)
willcodejavaforfood