【问题标题】:Key value coding crash - Objective-C键值编码崩溃 - Objective-C
【发布时间】:2013-02-14 00:02:40
【问题描述】:

我正在尝试创建一个 Objective-C 类。当我使用-setValue: 方法时,程序在该行崩溃。这是代码: 巨大的数字.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

程序为什么会崩溃?我做错了什么?

【问题讨论】:

    标签: objective-c key-value key-value-coding


    【解决方案1】:

    您错过了 .m 文件中的 setValueInString: 方法定义。

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 2013-02-07
      相关资源
      最近更新 更多