0

我在 cellForRowAtIndexPath 中使用以下代码,当我在 UILabel 中填充数据时,它正在发送错误

NSDictionary *benchmarkDictionary = (NSDictionary *)[benchmarkArray objectAtIndex:indexPath.row];

((UILabel *)[cell viewWithTag:15]).text = [benchmarkDictionary objectForKey:@"benchmarkTitle"];
((UILabel *)[cell viewWithTag:16]).text = [benchmarkDictionary objectForKey:@"benchmarkDescription"];

例外是:-[Benchmark objectForKey:]: unrecognized selector sent to instance 0x75d59c0 2013-03-08 16:21:39.515 Fdtrainer[2719:11303] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[Benchmark objectForKey :]: 无法识别的选择器发送到实例 0x75d59c0'

我的基准课程是:

@interface Benchmark : NSObject

@property (nonatomic, retain) NSString *bid;
@property (nonatomic, retain) NSString *benchmarkTitle;
@property (nonatomic, retain) NSString *benchmarkDescription;

-(id)initWithCoder: (NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

@implementation Benchmark

@synthesize bid;
@synthesize benchmarkTitle;
@synthesize benchmarkDescription;

- (id) initWithCoder:(NSCoder *)coder {

    self = [super init];
    if(self) {
        bid = [coder decodeObjectForKey:@"id"];
        benchmarkTitle = [coder decodeObjectForKey:@"benchmarkTitle"];
        benchmarkDescription = [coder decodeObjectForKey:@"benchmarkDescription"];
    }
    return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeObject:bid forKey:@"id"];
    [encoder encodeObject:benchmarkTitle forKey:@"benchmarkTitle"];
    [encoder encodeObject:benchmarkDescription forKey:@"benchmarkDescription"];
}

@end
4

2 回答 2

0

似乎以下行可能会导致问题。

NSDictionary *benchmarkDictionary = (NSDictionary *)[benchmarkArray objectAtIndex:indexPath.row];

你确定 benchmarkArray 里面包含 NSDictionary 吗?将某些内容投射到 NSDictionary 不会使其成为一体。看起来 benchmarkArray 包含 Benchmark 对象,它是 NSObject 类型的,它们不响应 objectForKey 选择器。

于 2013-03-09T02:03:04.637 回答
0

我猜您以错误的方式访问您的属性:

尝试这个 :

@interface Benchmark : NSObject

@property (nonatomic, retain) NSString *bid;
@property (nonatomic, retain) NSString *benchmarkTitle;
@property (nonatomic, retain) NSString *benchmarkDescription;

-(id)initWithCoder: (NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

@implementation Benchmark

@synthesize bid = _bid;
@synthesize benchmarkTitle = _benchmarkTitle;
@synthesize benchmarkDescription = _benchmarkDescription;

- (id) initWithCoder:(NSCoder *)coder {

self = [super init];
if(self) {
    _bid = [coder decodeObjectForKey:@"id"];
    _benchmarkTitle = [coder decodeObjectForKey:@"benchmarkTitle"];
    _benchmarkDescription = [coder decodeObjectForKey:@"benchmarkDescription"];
}
return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {

[encoder encodeObject:_bid forKey:@"id"];
[encoder encodeObject:_benchmarkTitle forKey:@"benchmarkTitle"];
[encoder encodeObject:_benchmarkDescription forKey:@"benchmarkDescription"];
}
于 2013-03-08T11:56:10.243 回答