【发布时间】:2012-11-14 14:22:28
【问题描述】:
嗨,当我使用 objectAtInded 方法从数组中检索 NSString 时,我似乎总是遇到异常。我正在从“PropertyList.plist”文件中的字典中读取数据。我的代码是
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
names = [[NSDictionary alloc]
initWithContentsOfFile:path];
keys = [[[names allKeys] sortedArrayUsingSelector:
@selector(compare:)] retain];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
异常发生在方法“cellForRowAtIndexPath”行中
cell.textLabel.text = [nameSection objectAtIndex:row];
错误信息是
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x6832440
plist 文件是
我在哪里使用“[nameSection objectAtIndex:row];”语句的类型它总是得到异常。
【问题讨论】:
-
因为这里编译器将 nameSection 作为 NSDictionary 而不是 NSArray。并且在字典对象上调用 objectAtIndex: 会引发异常。您必须交叉检查 PropertyList plist 的结构。
-
如何将nameSection的值作为一个数组?我试着做#Jasmeet Singh 所说的 NSMutableArray nameSection = (NSMutableArray)[names objectForKey:key];但它显示了同样的异常!
-
我是说要交叉检查您的 plist 结构。 [names objectForKey:key] 函数返回的是 NSDictionary 对象而不是 NSArray。
标签: ios xcode uitableview plist