年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2147|回复: 0

第七讲:Objective-C之Category

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2013-12-17 22:42:57 | 显示全部楼层 |阅读模式
    NSString+Reverse.h
    1. #import <Foundation/Foundation.h>
    2. @interface NSString(reverse)
    3. - (NSString *) reverseString;
    4. @end
    复制代码

    NSString+Reverse.m
    1. #import <Foundation/Foundation.h>
    2. #import "NSString+Reverse.h"
    3. @implementation NSString (reverse){
    4. - (id) reverseString{
    5.      NSUinteger len = [self length];
    6.      NSMutableString * retStr = [NSMutableString stringWithCapacity:len];
    7.      while(len>0){
    8.           unichar c = [self characterAtIndex:--len];
    9.           NSString * str = [NSString stringWithFormat:@"%C",c];
    10.           [retStr appendString:str];
    11.          }
    12.      return retStr;
    13.      }
    14. }
    15. @end
    复制代码

    main.m
    1. #import <Foundation/Foundation.h>
    2. #import "NSString+Reverse.h"
    3. int main(int argc, const char * argv[])
    4. {
    5.      NSAutoreleasePool = [[NSAutoreleasePool alloc] init];
    6.      NSString * str = [NSString stringWithString:@"Hello World"];
    7.      NSLog(@"String:%@",str);
    8.      NSString * rev = [str reverseString];
    9.      NSLog(@"Reversed:%@",rev);
    10.      [pool drain];
    11. }
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-23 20:43 , Processed in 0.043901 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表