读书人

关于Category的有关问题

发布时间: 2013-07-04 11:45:33 作者: rapoo

关于Category的问题
我应用category语法写了一个小程序,但xcode报错.
代码如下
NSString+reverse.h

#import <Cocoa/Cocoa.h>


@interface NSString (reverse)
-(NSString*)reverse();



@end


NSString+reverse.m
#import "NSString+reverse.h"


@implementation NSString(reverse)
-(NSString*)reverse(){
int length=[this length];
NSMutableString* reverse=[[NSString alloc] initWithCapacity:length];
while(length>0){
[reverse appendSting:[NSString stringWithFormat:@"%c",[self characterAtIndex:--length]]];
}
return [reverse autorelease];
}


@end


main.m
#import <Foundation/Foundation.h>
#import "NSString+reverse.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString* string=[[NSString alloc] init];
string=@"hello World";
NSLog(@"the reverse string is : %@",[string reverse]);
[pool drain];
return 0;
}

[解决办法]
NSMutableString* reverse=[[NSString alloc] initWithCapacity:length];
改为
NSMutableString* reverse=[[NSMutableString alloc] initWithCapacity:length];

另:
-(NSString*)reverse();
这是什么写法?强烈建议不要在oc中写c风格代码,为性能没办法的时候去做纯c的底层实现,-(NSString*)reverse;即可。。否则成函数了。
注意代码格式。。^^
[解决办法]
appendSting 应为 appendFormat

读书人网 >Iphone

热点推荐