Are there any shortcuts to (stringByAppendingString:
) string concatenation in Objective-C, or shortcuts for working with NSString
in general?
My preferred method is this:
NSString *firstString = @"foo";
NSString *secondString = @"bar";
NSString *thirdString = @"baz";
NSString *joinedString = [@[firstString, secondString, thirdString] join];
You can achieve it by adding the join method to NSArray with a category:
#import "NSArray+Join.h"
@implementation NSArray (Join)
-(NSString *)join
{
return [self componentsJoinedByString:@""];
}
@end
@[]
it's the short definition for NSArray
, I think this is the fastest method to concatenate strings.
If you don't want to use the category, use directly the componentsJoinedByString:
method:
NSString *joinedString = [@[firstString, secondString, thirdString] componentsJoinedByString:@""];