- [__ NSCFArray objectAtIndex:]:index(1)超出边界(1)

时间:2012-12-17 02:23:53

标签: iphone ios ios6 plist

在我添加第1项和第2项之前,我的plist正好使用下面的代码。

然后随着项目的进展,我必须在我的plist中添加几个项目。然后发生错误。

问题是,我有一个'Root'plist,它包含Item 0,Item 1和Item 2作为Dictionary,如下所述:

顺便说一句,除了省内的字符串外,plists的数据相同。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Province</key>
        <string>Metro Manila</string>
        <key>Cities</key>
        <array>
            <dict>
                <key>Places</key>
                <string>Chowking</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>Province</key>
        <string>Pampanga</string>
        <key>Cities</key>
        <array>
            <dict>
                <key>Places</key>
                <string>Jollibee</string>
            </dict>
            <dict>
                <key>Places</key>
                <string>McDonald's</string>
            </dict>
            <dict>
                <key>Places</key>
                <string>Pizza Hut</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>Province</key>
        <string>Pangasinan</string>
        <key>Cities</key>
        <array>
            <dict>
                <key>Places</key>
                <string>Jollibee</string>
            </dict>
            <dict>
                <key>Places</key>
                <string>McDonald's</string>
            </dict>
        </array>
    </dict>
</array>
</plist>

这是我的Controller.m

的代码
//
//  RootViewController.m
//  TableView
//
//  Created by OSX on 10/10/12.
//  Copyright (c) 2012 OSX. All rights reserved.
//

#import "RootViewController.h"
#import "CityViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController {
    NSArray * luzonRegion;
    NSArray * visayasRegion;
    NSArray * mindanaoRegion;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Region";

    [self loadRegionPlist];
}

- (void)loadRegionPlist
{
    NSString *plistLuzon    = [[NSBundle mainBundle] pathForResource: @"luzonPlist" ofType: @"plist"];
    luzonRegion   = [[NSArray alloc] initWithContentsOfFile: plistLuzon];
    NSString *plistVisayas  = [[NSBundle mainBundle] pathForResource: @"visayasPlist" ofType: @"plist"];
    visayasRegion = [[NSArray alloc] initWithContentsOfFile: plistVisayas];
    NSString *plistMindanao = [[NSBundle mainBundle] pathForResource: @"mindanaoPlist" ofType: @"plist"];
    mindanaoRegion = [[NSArray alloc] initWithContentsOfFile: plistMindanao];

    NSLog(@"%@", luzonRegion);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return [luzonRegion count];
            break;

        case 1:
            return [visayasRegion count];
            break;

        case 2:
            return [mindanaoRegion count];
            break;

        default:
            break;
    }

    return section;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Configure the cell...
    NSDictionary *luzonDictionary    = [luzonRegion    objectAtIndex:indexPath.row];
    NSDictionary *visayasDictionary  = [visayasRegion  objectAtIndex:indexPath.row];
    NSDictionary *mindanaoDictionary = [mindanaoRegion objectAtIndex:indexPath.row];

    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = [luzonDictionary    objectForKey: @"Province"];
            break;

        case 1:
            cell.textLabel.text = [visayasDictionary  objectForKey: @"Province"];
            break;

        case 2:
            cell.textLabel.text = [mindanaoDictionary objectForKey: @"Province"];
            break;

        default:
            break;
    }

    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return @"Luzon";
            break;

        case 1:
            return @"Visayas";
            break;

        case 2:
            return @"Mindanao";
            break;

        default:
            break;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityViewController *cityViewController = [[CityViewController alloc] initWithStyle:UITableViewStylePlain];

    NSDictionary *luzonDictionary    = [luzonRegion    objectAtIndex:indexPath.row];
    NSDictionary *visayasDictionary  = [visayasRegion  objectAtIndex:indexPath.row];
    NSDictionary *mindanaoDictionary = [mindanaoRegion objectAtIndex:indexPath.row];
    switch (indexPath.section) {
        case 0:
            cityViewController.title  = [luzonDictionary   objectForKey: @"Province"];
            cityViewController.Cities = [luzonDictionary   objectForKey: @"Cities"];
            break;

        case 1:
            cityViewController.title  = [visayasDictionary  objectForKey: @"Province"];
            cityViewController.Cities = [visayasDictionary  objectForKey: @"Cities"];
            break;

        case 2:
            cityViewController.title  = [mindanaoDictionary objectForKey: @"Province"];
            cityViewController.Cities = [mindanaoDictionary objectForKey: @"Cities"];
            break;

        default:
            break;
    }

    [self.navigationController pushViewController:cityViewController animated:YES];
}

@end

NSLog和调试区输出:

2012-12-17 11:24:21.023 TableViewPlist[24315:c07] (
        {
        Cities =         (
                        {
                Places = Chowking;
            }
        );
        Province = "Metro Manila";
    },
        {
        Cities =         (
                        {
                Places = Jollibee;
            },
                        {
                Places = "McDonald's";
            },
                        {
                Places = "Pizza Hut";
            }
        );
        Province = Pampanga;
    },
        {
        Cities =         (
                        {
                Places = Jollibee;
            },
                        {
                Places = "McDonald's";
            }
        );
        Province = Pangasinan;
    }
)

2012-12-17 11:24:21.043 TableViewPlist[24315:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
*** First throw call stack:
(0x1c94012 0x10d1e7e 0x1c93deb 0x1c887e0 0x36aa 0xd3f4b 0xd401f 0xbc80b 0xcd19b 0x6992d 0x10e56b0 0x2290fc0 0x228533c 0x2290eaf 0x1088cd 0x511a6 0x4fcbf 0x4fbd9 0x4ee34 0x4ec6e 0x4fa29 0x52922 0xfcfec 0x49bc4 0x49dbf 0x49f55 0xc472d84 0x52f67 0x2cb2 0x167b7 0x16da7 0x17fab 0x29315 0x2a24b 0x1bcf8 0x1befdf9 0x1befad0 0x1c09bf5 0x1c09962 0x1c3abb6 0x1c39f44 0x1c39e1b 0x177da 0x1965c 0x293d 0x2865 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

2 个答案:

答案 0 :(得分:1)

问题似乎在这里:

NSDictionary *luzonDictionary    = [luzonRegion    objectAtIndex:indexPath.row];
NSDictionary *visayasDictionary  = [visayasRegion  objectAtIndex:indexPath.row];
NSDictionary *mindanaoDictionary = [mindanaoRegion objectAtIndex:indexPath.row];

如果其中一个区域不包含与其他区域一样多的项目,则会失败。

答案 1 :(得分:0)

我找到了!在cellForRowAtIndexPath中,就像Nickolay O.所说的那样!

我把它改为:

switch (indexPath.section) {
    case 0:
        cell.textLabel.text = [[luzonRegion objectAtIndex: indexPath.row] objectForKey: @"Province"];
        break;

    case 1:
        cell.textLabel.text = [[visayasRegion objectAtIndex: indexPath.row] objectForKey: @"Province"];
        break;

    case 2:
        cell.textLabel.text = [[mindanaoRegion objectAtIndex: indexPath.row] objectForKey: @"Province"];
        break;

    default:
        break;
}