gpt4 book ai didi

ios - 无法从IOS中的plist文件读取字典数据

转载 作者:行者123 更新时间:2023-12-01 19:15:54 24 4
gpt4 key购买 nike

我在“支持文件”文件夹中有PropertyList.plist文件。我在里面做了字典。 plist文件是:

我的ViewController.m文件代码是

   @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.count”发出了警报,该计数值为零。而且“名称”计数也为零。 vieDidLoad方法上的path变量显示正确的路径。但无法从plist文件的字典中读取。

编辑:我使用了nslog,它表明在“viewDidLoad”方法中,“名称”能够加载字典。但是“键”数组无法加载。

最佳答案

@EugeneK所说的有效,但是在线上的“cellForRowAtIndexPath”方法中却得到了肯定

cell.textLabel.text = [nameSection objectAtIndex:row];

错误消息是

“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例0x6832440”。

问题是nameSection变量显示为不支持objectAtIndex方法的NSDictionary类型对象。

所以我所知道的不是很好的方法。我相信还有其他方法。我将'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);
}

有用!任何想法如何做得更好,但将不胜感激。

关于ios - 无法从IOS中的plist文件读取字典数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13359344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com