【问题标题】:App crashing on iPad but not simulator应用程序在 iPad 上崩溃但不是模拟器
【发布时间】:2013-04-04 06:03:57
【问题描述】:

我的应用程序在 iPad 上运行时崩溃,但在 iPad 模拟器上运行 100% 我在 iPad 上使用的是 Xcode 4.6.1 版和 6.1.3 版。 问题在于我试图在 segues 之间传递 int 的值

在我的 .h 中

@property (nonatomic, assign)int currentQuestion;

在我的 .m 中

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"level1correct"]){
    AddLevel1IncorrectViewController *incorrect = [segue destinationViewController];
    incorrect.CQValue = self.currentQuestion;
}}

AddLevel1Incorrect.h

@property (nonatomic, assign)int CQValue;

AddLevel1Incorrect.m

@synthesize CQValue = _CQValue;

- (void)imageSelect{
int numItems = [arrayPath count];
NSMutableArray *left = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *right = [NSMutableArray arrayWithCapacity:numItems];

for (NSDictionary *itemData in arrayPath) {
    [left addObject:[itemData objectForKey:@"L"]];
    [right addObject:[itemData objectForKey:@"R"]];
}

NSLog(@" value of %d CQValue ", self.CQValue);
leftImageViewer.image = [UIImage imageNamed:left[self.CQValue]];//this is the point where the crash happens
rightImageViewer.image = [UIImage imageNamed:right[self.CQValue]];
}

有趣的是,它确实在控制台的 NSLog 中显示了正确的值,正如您将在崩溃消息顶部看到的那样

2013-04-03 22:50:00.404 thefyp[1506:907]  value of 1 CQValue 
2013-04-03 22:50:00.408 thefyp[1506:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:

任何想法我在这里出错了吗?

【问题讨论】:

    标签: ios xcode crash int segue


    【解决方案1】:

    您的代码非常脆弱,这意味着它对正在使用的数据做出了很多假设,而没有验证数据是否准确。

    在访问数组之前,您永远不会检查数组的边界。 self.CQValue 是 1,但在这种情况下,数组本身是空的。所以left[self.CQValue],就是left[1],无效。

    检查arrayPath 以确保它不为空。

    【讨论】:

    • 我应该如何使用 NSLog 检查它是否为空或是否存在?
    • 会不会是它崩溃的原因
    • 它崩溃了,因为 CQValue 为 1 但数组为空。由于某种原因,arrayPath 没有设置。
    • 我不认为是这样,因为我在 viewDidLoad 中有一个函数,它再次加载数组arrayPath = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Addition1" ofType:@"plist"]]; [self imageSelect]; 我创建了一个 NSLog,它正确显示了 arrayPath 的完整内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多