0

我是 Objective-c 的新手,所以我有点难过,我在我的应用程序的另一部分有它,并且工作完美,但是当我在我的应用程序的不同视图上使用它时,它会使应用程序崩溃,我很确定我以相同的方式设置它。

我让 Xcode 在搞砸的代码和这段代码的第一行设置断点

    NSArray *timesArray = [self.tableDataArray objectAtIndex:2];
    NSDictionary *dictionary = [timesArray objectAtIndex:indexPath.row];
    cell.timeLabel.text = [dictionary objectForKey:@"time"];
    cell.destinationLabel.text = [dictionary objectForKey:@"destination"];

NSArray *timesArray 是停止的地方,但没有给出其他错误或任何东西,所以我有点不知道要改变什么。

编辑编号 1:为清楚起见,删除了旧编辑。

编辑编号 2

好的,所以我再次慢慢检查,结果是它对数据进行了 3 次完整解析,并输出了我拥有的所有内容。

2012-06-21 13:37:09.148 NextBus[5990:1a603] Array: (
    {
    routeName = "Oxford West";
    routeNumber = 17;
    stopDirection = Westbound;
    stopName = "Griffith At Commissioners Sb";
    stopNumber = 811;
},
"Next 3 Vehicles Arrive At:",
    (
            {
        destination = "To Byron Baseline / Griffith";
        time = "1:58 P.M.";
    },
            {
        destination = "To Byron Baseline / Griffith";
        time = "2:15 P.M.";
    },
            {
        destination = "To Byron Baseline / Griffith";
        time = "2:35 P.M.";
    }
),
"Prediction Last Updated 1:37:06 Pm 6/21/2012"
)
2012-06-21 13:37:09.148 NextBus[5990:1a603] Array: (
    {
    routeName = "Oxford West";
    routeNumber = 17;
    stopDirection = Westbound;
    stopName = "Griffith At Commissioners Sb";
    stopNumber = 811;
},
"Next 3 Vehicles Arrive At:",
    (
            {
        destination = "To Byron Baseline / Griffith";
        time = "1:58 P.M.";
    },
            {
        destination = "To Byron Baseline / Griffith";
        time = "2:15 P.M.";
    },
            {
        destination = "To Byron Baseline / Griffith";
        time = "2:35 P.M.";
    }
),
"Prediction Last Updated 1:37:06 Pm 6/21/2012"
)
2012-06-21 13:37:09.149 NextBus[5990:1a603] Array: (
    {
    routeName = "Oxford West";
    routeNumber = 17;
    stopDirection = Westbound;
    stopName = "Griffith At Commissioners Sb";
    stopNumber = 811;
},
"Next 3 Vehicles Arrive At:",
    (
            {
        destination = "To Byron Baseline / Griffith";
        time = "1:58 P.M.";
    },
            {
        destination = "To Byron Baseline / Griffith";
        time = "2:15 P.M.";
    },
            {
        destination = "To Byron Baseline / Griffith";
        time = "2:35 P.M.";
    }
),
"Prediction Last Updated 1:37:06 Pm 6/21/2012"
)

当我拉动刷新它时

2012-06-21 13:37:45.590 NextBus[5990:1a603] Array: (
    {
    routeName = "Oxford West";
    routeNumber = 17;
    stopDirection = Westbound;
    stopName = "Griffith At Commissioners Sb";
    stopNumber = 811;
}
)

然后由于某种原因崩溃,它停止解析。

它是 EGO 拉动刷新套件。

4

1 回答 1

0

编辑:

当您的查询返回的数据不足时,似乎会发生崩溃,例如,当没有时间可用于下一班车时。如果您可以确认这一点,那么修复是微不足道的:

if ([self.tableDataArray count] > 1) {
  NSArray *timesArray = [self.tableDataArray objectAtIndex:2];
  NSDictionary *dictionary = [timesArray objectAtIndex:indexPath.row];
  cell.timeLabel.text = [dictionary objectForKey:@"time"];
  cell.destinationLabel.text = [dictionary objectForKey:@"destination"];
} else {
  //-- do something to show that no times are available
}

您可能试图访问超出其边界的数组:

    NSArray *timesArray = [self.tableDataArray objectAtIndex:2];

timesArray没有3个元素,然后程序崩溃......

您应该查看填充数组的方式与您对此操作的期望。

编辑:

根据您对问题所做的编辑,您似乎正在执行您多次发布的行,并且只是最后一次程序崩溃。如您所见,在前几次迭代中,您的数组填充了足够的元素;在最后一种情况下:

2012-06-21 13:25:03.448 NextBus[5964:1a603] Array: (
{
routeName = Springbank;
routeNumber = 5;
stopDirection = Eastbound;
stopName = "Berkshire At Berkshire Place Sb";
stopNumber = 2196;
}

数组只有一个对象。这是一个你应该以不同方式处理的极端情况吗?

于 2012-06-21T17:30:01.100 回答