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

标签 ios xcode uitableview plist

我在“支持文件”文件夹中有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/

相关文章:

ios - UITableViewCell 中的数据源委托(delegate)

ios - 如何取消隐藏 UITableView 部分的动画?

iphone - 最终 TableView :heightForRowAtIndexPath: solution

c++ - 有什么方法不需要在 Xcode 中使用文件的完整路径? [C/C++]

ios - 使用 Xcode 8.1 不接收推送通知

ios - Facebook 应用程序类型 : Which one to pick for mobile AND web?

ios - iOS 问题中的自定义警报 View

ios - 如何在 ios swift 项目中为 dev n prod 使用两个不同的 GoogleService-info.plist 文件?

ios - 如何使用 SwiftUI 在另一个 View 中创建 View ?

ios - UIButton 的子类无法调用 buttonType 初始值设定项