2011-05-05 94 views

回答

6

定義的NSMutableString

@implementation NSMutableString (AppendPrefix) 

- (void)appendPrefix:(NSString *)prefix { 
    [self insertString:prefix atIndex:0]; 
} 

@end 

然後類別做:

[array makeObjectsPerformSelector:@selector(appendPrefix:) withObject:@"some_prefix"]; 

array這裏是NSMutableString

1

NSArray這是一個有點冗長,但這應該工作:

- (NSArray *) prependArrayOfStrings:(NSArray*)originalArray prefix:(NSString*)prefix 
{ 
    NSMutableArray *newArray = [[[NSMutableArray alloc] init] autorelease]; 
    for(NSString *currString in originalArray) 
    { 
    NSString *newString = [NSString stringwithFormat:@"%@%@", prefix, currString]; 
    [newArray addObject:newString]; 
    } 

    return newArray; 
} 

// Somewhere else. 
NSArray *originalArray = @[/*...*/]; 
// Fill in original array with array of strings 
NSArray *newArray = [self prependArrayOfStrings:originalArray prefix:@"prefix"]; 
相關問題