我正在尝试创建一个Objective-C类。当我使用-setValue:
方法时,程序在该行崩溃。这是代码:
HugeNumber.h:
#import <Foundation/Foundation.h>
@interface HugeNumber : NSObject{
NSString *value;
NSMutableArray *charSetArray;
}
-(void)setValueInString:(NSString *)string;
-(NSString *)valueInString;
-(int)valueInInt;
-(NSString *)decimalValueInString;
-(void)setDecimalValueInString;
@end
HugeNumber.m:
#import "HugeNumber.h"
const NSString* charSet = @"0123456789abcdefghijklmnepqrstuvwxyz";
@implementation HugeNumber
- (id)init{
self = [super init];
if (self){
for (int i = 0; i < [charSet length]; i++){
NSString* str = [NSString stringWithFormat:@"%hu",[charSet characterAtIndex:i]];
[charSetArray addObject:str];
}
}
return self;
}
-(void)setValue:(NSString *)string{
value = string;
}
@end
main.m:
#import <Foundation/Foundation.h>
#import "HugeNumber.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
HugeNumber *number = [[HugeNumber alloc] init];
NSString *string = @"11";
[number setValueInString:string];
NSLog(@"%i",[number valueInInt]);
}
return 0;
}
我得到的错误日志是:
2013-02-13 17:56:43.303 Huge Numb Test[7442:303] -[HugeNumber setValueInString:]: unrecognized selector sent to instance 0x10010a620
2013-02-13 17:56:43.306 Huge Numb Test[7442:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HugeNumber setValueInString:]: unrecognized selector sent to instance 0x10010a620'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8a15b0a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff90c5e3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a1f16ea -[NSObject(NSObject) doesNotRecognizeSelector:] + 186
3 CoreFoundation 0x00007fff8a1495ce ___forwarding___ + 414
4 CoreFoundation 0x00007fff8a1493b8 _CF_forwarding_prep_0 + 232
5 Huge Numb Test 0x00000001000018a1 main + 129
6 libdyld.dylib 0x00007fff8cfa17e1 start + 0
7 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminate called throwing an exception
为什么程序崩溃了?我做错了什么?
答案 0 :(得分:1)
您在.m文件中错过了setValueInString:
方法定义。