objectAtIndex:indexPath.row方法总是在IOS中导致异常

时间:2012-11-14 04:45:44

标签: ios xcode uitableview plist

当我使用objectAtInded方法从数组中检索NSString时,我似乎总是遇到异常。我正在从“PropertyList.plist”文件中的字典中读取数据。我的代码是

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

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


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

中的方法“cellForRowAtIndexPath”发生异常
cell.textLabel.text = [nameSection objectAtIndex:row];

错误消息是

  

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

plist文件是 enter image description here

我在哪里使用“[nameSection objectAtIndex:row];”它总是得到例外的声明类型。

3 个答案:

答案 0 :(得分:2)

原因可以是

  1. [name objectForKey:key] ;.这个语句可以给输出一个NSMutableDictionary类型,你从中获取NSArray。 OR

  2. 如果是数组,则使用以下代码获取nameSection

       NSMutableArray *nameSection = (NSMutableArray*)[names objectForKey:key];
       // using (NSMutableArray*) before the code is for external typecasting to tell the compiler that the output is of NSMutableArray type.
    
  3. 希望这有帮助。

    编辑: -

    使用以下方法从plist文件中获取字典

            -(NSMutableDictionary *) GetDictDataFromPlistFile:(NSString *) fileName
           {
                 NSError *error;
                 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
                 NSString *documentsDirectory = [paths objectAtIndex:0]; //2
                 NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]]; //3
    
                  NSFileManager *fileManager = [NSFileManager defaultManager];
    
                if (![fileManager fileExistsAtPath: path]) //4
                {
                       NSString *bundle = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; //5
                      [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
                 }
    
                  NSMutableDictionary *dictData =   [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    
                   return dictData;
            }
    

答案 1 :(得分:1)

此处,name仅包含root的一个键。 name所需的是键Root的值。 请重试!

答案 2 :(得分:0)

与@scorpiozj一样,'names'只包含'Root'的键。所以我所知道的并不是一个很好的方法。我相信还有其他方法。我将'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);
}

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