首页 > 代码库 > Objective C 代码片段(类别)

Objective C 代码片段(类别)

1 @interface NSString (reverse)
2 
3 -(NSString *) reverseString;
4 
5 @end
 1 @implementation NSString (reverse)
 2  
 3 -(NSString *) reverseString {
 4     int length = [self length];
 5     NSMutableString *reversedString;
 6     
 7     reversedString = [[NSMutableString alloc] initWithCapacity: length];
 8     
 9     while(length > 0) {
10         [reversedString appendString:[NSString stringWithFormat:@"%C", [self characterAtIndex:--length]]];
11     }
12     return [reversedString autorelease]; 
13 }    
14  @end