无法从IOS中的plist文件读取字典数据
我在“支持文件”文件夹中有PropertyList.plist文件。我在里面编了一本字典。plist文件是: 我的ViewController.m文件代码为无法从IOS中的plist文件读取字典数据,ios,xcode,uitableview,plist,Ios,Xcode,Uitableview,Plist,我在“支持文件”文件夹中有PropertyList.plist文件。我在里面编了一本字典。plist文件是: 我的ViewController.m文件代码为 @implementation GroupedInexedViewController { NSDictionary *names; NSArray *keys; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];
@implementation GroupedInexedViewController
{
NSDictionary *names;
NSArray *keys;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]
initWithContentsOfFile:path];
names = dict;
[dict release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:
@selector(compare:)];
keys = array;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
-(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;
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
return key;
}
不幸的是,“keys”数组不包含任何元素。因为我发出了一个警报,它的计数值是“keys.count”,它是零。而且“姓名”计数也为零。vieDidLoad方法上的path变量显示正确的路径。但它无法从plist文件的字典中读取
编辑:我使用了nslog,它显示在“viewDidLoad”方法中,“names”能够加载字典。但是“keys”数组无法加载它 是对的。请参阅代码中的注释:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]
initWithContentsOfFile:path]; //retainCount of dict is 1
names = dict; // you made weak reference to dict
[dict release]; // retainCount is 0 - dict is being dealloced
NSArray *array = [[names allKeys] sortedArrayUsingSelector:
@selector(compare:)]; // you try to get data from dealloced object
keys = array;
}
尝试执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
names = [[NSDictionary alloc]
initWithContentsOfFile:path];
keys = [[[names allKeys] sortedArrayUsingSelector:
@selector(compare:)] retain];
}
不要忘记在视图控制器中释放
名称
和键
EugeneK所说的有效,但在“CellForRowatineXpath”方法中得到了sigabrt
cell.textlab.text=[nameSection objectAtIndex:row]
错误消息是
“由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:”-[\uu NSCFDictionary objectAtIndex:]:未识别的选择器发送到实例0x6832440”
问题是nameSection变量显示为NSDictionary类型的对象,它不支持objectAtIndex方法
所以我知道我所做的不是一个很好的方法。我相信还有别的办法。我将“viewDidLoad”方法更改为
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
names = [[NSDictionary alloc]
initWithContentsOfFile:path];
keys = [names allKeys];
NSString *key = [keys objectAtIndex:0];
names = [names objectForKey:key];
keys = [[[names allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];
NSLog(@"keys = %@ names = %@",keys,names);
}
它起作用了!不过,如果你能想出更好的办法,我们将不胜感激。试试你的字典。它会显示你的.plist吗?是的,它显示如下根={A=(A,AA);B=(B,BB);C=(C,CC);};NSArray*数组=[[names allKeys]SortedArray使用选择器:@selector(比较:)];但是这个数组没有显示任何内容!我不是100%确定(因为我总是使用自动引用计数),但我认为
[dict release]
是错误的。我的项目没有启用自动引用计数:)在这里,当我释放'dict'变量时,是否意味着值也从'names'中删除?绝对保留计数没有意义。使用Delta。感谢它可以这样做,但在“cell.textlab.text=[nameSection objectAtIndex:row]”行的“cellforrowatinexpath”方法中获取sigabrt;错误消息是“由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[\u NSCFDictionary objectAtIndex::未识别的选择器发送到实例0x6832440”