简体   繁体   English

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

[英]Cant read dictionary data from plist file in IOS

I have PropertyList.plist file in the "Supporting Files" folder. 我在“Supporting Files”文件夹中有PropertyList.plist文件。 I have made a dictionary in it. 我在里面写了一本字典。 The plist file is: plist文件是:

在此输入图像描述

MY ViewController.m file code is 我的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;
}

Unfortunately the "keys" array doesnt contain any element. 不幸的是,“keys”数组不包含任何元素。 Because I made an alert with its count value which is "keys.count" and it was zero. 因为我用其计数值“keys.count”发出警报并且它为零。 and also the "names" count was also zero. 并且“名字”计数也是零。 The path variable on vieDidLoad method shows the correct path. vieDidLoad方法上的路径变量显示正确的路径。 but it cant read from the dictionary of the plist file's. 但它无法从plist文件的字典中读取。

Edit: I used nslog and it shows that in "viewDidLoad" method "names" is able to load the dictionary . 编辑:我使用了nslog,它显示在“viewDidLoad”方法中“names”能够加载字典。 but "keys" array is unable to load it. 但“keys”数组无法加载它。

What @EugeneK said worked but got sigabrt in "cellForRowAtIndexPath" method on the line 什么@EugeneK说的工作,但在线上的“cellForRowAtIndexPath”方法得到了sigabrt

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

the error message is 错误信息是

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6832440". “由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x6832440”。

The problem was nameSection variable was appearing as a NSDictionary type object which doesn't support objectAtIndex method. 问题是nameSection变量出现为NSDictionary类型对象,它不支持objectAtIndex方法。

So what I did I know is not a very good way to do it. 所以我所知道的并不是一个很好的方法。 I am sure there is some other way. 我相信还有其他方法。 I changed the 'viewDidLoad' method to this, 我将'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);
}

It works! 有用! Any idea how to do it better will be appreciated though. 任何想法如何做得更好将不胜感激。

Martin R is right. 马丁R是对的。 Please see comments in your code: 请参阅代码中的注释:

- (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;
}

Try to do the following: 尝试执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
                                                     ofType:@"plist"];
    names = [[NSDictionary alloc]
             initWithContentsOfFile:path];

    keys = [[[names allKeys] sortedArrayUsingSelector:
            @selector(compare:)] retain];
}

And do not forgot to release names and keys in your view controller dealloc . 并且不要忘记在视图控制器dealloc释放nameskeys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM