// // NSString+Reverse.h // OC5_类别 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> @interface NSString (Reverse) + (NSString *)reverseString:(NSString *)aString; + (void)printString:(NSString *)aString; @end
// // NSString+Reverse.m // OC5_类别 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "NSString+Reverse.h" //类名+类别名.h //类名+类别名.m @implementation NSString (Reverse) + (NSString *)reverseString:(NSString *)aString { NSString *str = @""; NSInteger len = [aString length]; for (NSInteger i=len-1; i>=0; i--) { str = [str stringByAppendingFormat:@"%C",[aString characterAtIndex:i]]; } return str; } + (void)printString:(NSString *)aString { NSLog(@"%@", aString); } @end
// // main.m // OC5_类别 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "NSString+Reverse.h"//使用类别中的方法, 必须包含对应的头文件 //NSString //类别 //给类或者类族扩展方法 //但是不能扩展实例变量 int main(int argc, const char * argv[]) { @autoreleasepool { [NSString printString:[NSString reverseString:@"hello world"]]; } return 0; }
时间: 2024-10-13 05:54:11