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

时间:2012-11-13 10:46:18

标签: ios xcode uitableview plist

我在“Supporting Files”文件夹中有PropertyList.plist文件。我在里面写了一本字典。 plist文件是:

enter image description here

我的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”数组不包含任何元素。因为我用其计数值“keys.count”发出警报并且它为零。并且“名字”计数也是零。 vieDidLoad方法上的路径变量显示正确的路径。但它无法从plist文件的字典中读到。

编辑:我使用了nslog,它显示在“viewDidLoad”方法中“names”能够加载字典。但“keys”数组无法加载它。

2 个答案:

答案 0 :(得分:2)

@EugeneK说的是什么工作但是在行上的“cellForRowAtIndexPath”方法中获得了sigabrt

  

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

错误消息是

  

“因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:' - [__ 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);
}

有效!不过,任何想法如何做得更好都会受到赞赏。

答案 1 :(得分:1)

Martin R是对的。请参阅代码中的注释:

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

不要忘记在视图控制器names中发布keysdealloc