你的位置:首页 > 操作系统

[操作系统]类目(分类)

分类(类目)--字符串逆序输出


 

 

目录

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--) {    // 字符串的倒序    char ch=[self characterAtIndex:i];    // 字符串的添加    [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";    // 逆序输出    NSLog(@"%@",[str reverse]);        NSLog(@"%d",[str lengthAsString].intValue);  }  return 0;}

运行结果

类目(分类)---集合逆序输出


 

目录

 

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 // 逆序输出方法实现{  NSMutableArray *array=[NSMutableArray array];  // 小于集合的个数  for (int i=(int)self.count-1; i>=0; i--) {    // 集合的添加用addObject 集合的倒序objectAtIndex    [array addObject:[self objectAtIndex:i]];  }  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:@"a",@"b",@"c",@"d",@"e", nil];   NSArray *arr=[array reverse];    NSLog(@"%@",arr);  }  return 0;}

运行结果: