我应用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;
}
代码如下
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;
}
解决方案 »
- GCC可以编译OC语言么?
- UITableView滚动条位置
- 请问如何求两个向量的夹角度数呀?
- xcode4.2 for lion不知道如何设置
- EXC_BAD_INSTRUCTION 错误,请大家帮忙!
- 经过设置,启动显示不了tabbar control,请指教
- IOS开发下,要怎么调试界面?需要买齐设备来调试吗?
- 怎样让uiwebview里 点击的网页链接 支持uinavigationbar的返回操作?
- c语言数组问题,求大神指导小弟
- 新人求教,如何在IOS系统上开发出VPN的客户端?
- IBOutlet是啥类型?
- The identity 'iPhone Developer' doesn't match any valid, non-expired certificate
改为
NSMutableString* reverse=[[NSMutableString alloc] initWithCapacity:length];另:
-(NSString*)reverse();
这是什么写法?强烈建议不要在oc中写c风格代码,为性能没办法的时候去做纯c的底层实现,-(NSString*)reverse;即可。。否则成函数了。
注意代码格式。。^^
NSString+reverse.h
#import <Cocoa/Cocoa.h>
@interface NSString (reverse)
-(NSString*)reverse;
@endNSString+reverse.m
#import "NSString+reverse.h"
@implementation NSString(reverse)
-(NSString*)reverse{
int length=[self length];
NSMutableString* reverse=[[NSMutableString 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;
}