我正在制作一个尝试从.plist文件中读取信息的应用程序(放在那里解析的JSON)。
从文件中读取的流程很好:得到了字典数组,但是在尝试在 tableview 上显示它时,问题就开始了。初始视图已正确加载,但当我开始滚动时,应用程序崩溃。
#define DOCUMENTS [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePathDocArray = [DOCUMENTS stringByAppendingPathComponent:@"filters.plist"];
NSString *filePathBundleArray = [[NSBundle mainBundle] pathForResource:@"filters" ofType:@"plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePathDocArray]) {
[[NSFileManager defaultManager] copyItemAtPath:filePathBundleArray toPath:filePathDocArray error:nil];
NSLog(@"File saved");
} else {
NSLog(@"File already exists");
filters = [NSArray arrayWithContentsOfFile:filePathDocArray];
}
}
在这里,我将所需的所有信息放入过滤器数组(通过循环检查)。然后:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filters count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myIdentifier];
}
NSInteger crow = indexPath.row;
NSDictionary *story = [filters objectAtIndex: crow];
cell.textLabel.text = [story objectForKey:@"Name"];
cell.detailTextLabel.text = [story objectForKey:@"Description"];
return cell;
}
@end
当应用程序启动时一切正常:我看到了 normel 表格视图,但是当我开始滚动时它崩溃了
在我评估了一系列断点调试之后,在模拟器上启动应用程序后,数组过滤器上的链接会拧紧,所以当我尝试填充下一个单元格时,无法正确创建故事字典。
这可能是什么问题?
这里控制台报告:
2012-09-22 13:37:43.545 JSONExample[4559:207] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a083c0
2012-09-22 13:37:43.547 JSONExample[4559:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a083c0'