2013-03-08 52 views
0

我使用以下代码的异常,当我在的UILabel填充数据被发送错误的UITableViewCell生成对的cellForRowAtIndexPath对细胞项目设定文本

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

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

例外是: - [基准objectForKey:]:无法识别选择发送到实例0x75d59c0 2013年3月8日16:21:39.515 Fdtrainer [2719:11303] *终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因:“ - [基准objectForKey:]:无法识别的选择发送到实例0x75d59c0'

我的Benchmark类是:

@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 

回答

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"]; 
} 
+0

仍给它同样的错误 – 2013-03-08 14:06:22

0

好像下面的行可能会引起问题。

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

你确定benchmarkArray里面包含NSDictionary。将东西铸造到NSDictionary不会让它成为一个。看起来像benchmarkArray包含Benchmark对象,它是NSObject类型的,它们不响应objectForKey选择器。