Category (category)
Category-string reverse output
Directory
NSString + Reverse. h
#import <Foundation/Foundation.h>@interface NSString (Reverse)-(NSString *)reverse;@end
NSString + Reverse. m
# Import "NSString + Reverse. h "@ implementation NSString (Reverse)-(NSString *) reverse {NSMutableString * str = [NSMutableString string]; // for (int I = (int) self. length-1; I> = 0; I --) {// reverse char ch of the string = [self characterAtIndex: I]; // Add a string to [str appendFormat: @ "% c", ch];} return str ;}@ end
NSString + Catel. h
#import <Foundation/Foundation.h>@interface NSString (Catel)-(NSNumber *)lengthAsString;@end
NSString + Catel. m
#import "NSString+Catel.h"@implementation NSString (Catel)-(NSNumber *)lengthAsString{ int len= (int)self.length; return [NSNumber numberWithInt:len];}@end
Main. m
# Import <Foundation/Foundation. h> # import "NSString + Reverse. h "# import" NSString + Catel. m "int main (int argc, const char * argv []) {@ autoreleasepool {NSString * str = @" abc "; // output NSLog in reverse order (@" % @", [str reverse]); NSLog (@ "% d", [str lengthAsString]. intValue);} return 0 ;}
Running result
Category (Category) --- set reverse output
Directory
NSArray + Reversre. h
#import <Foundation/Foundation.h>@interface NSArray (Reversre)-(NSArray *)reverse;@end
NSArray + Reversre. m
# Import "NSArray + Reversre. h "@ implementation NSArray (Reversre)-(NSArray *) reverse // implement the reverse output method {NSMutableArray * array = [NSMutableArray array]; // less than the number of sets for (int I = (int) self. count-1; I> = 0; I --) {// Add a set using the inverted objectAtIndex [array addObject: [self objectAtIndex: I] of the addObject set;} return array;} @ end
Main. m
# Import <Foundation/Foundation. h> # import "NSArray + Reversre. h "int main (int argc, const char * argv []) {@ autoreleasepool {// NSArray * array = [NSArray arrayWithObjects: @" ", @ "B", @ "c", @ "d", @ "e", nil]; NSArray * arr = [array reverse]; NSLog (@ "% @", arr);} return 0 ;}
Running result: