2013-03-08 71 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選擇器。

相關問題