美文网首页
崩溃集锦

崩溃集锦

作者: 哲逗年 | 来源:发表于2018-01-12 10:24 被阅读0次

数组越界

NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
NSLog(@"index:%@---noneDataIndex:%zd",@"1",@"4");
[arr exchangeObjectAtIndex:[arr indexOfObject:@"1"] withObjectAtIndex:[arr indexOfObject:@"4"]];

打印结果:

index:1---noneDataIndex:4402863976
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM exchangeObjectAtIndex:withObjectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 2]'

原因分析:获取数组中一个不存在的对象的index时,获取到的值是一个比较大随机数值,在使用exchangeObjectAtIndex时就会产生崩溃现象,所以要做提前做好判断。

相关文章

网友评论

      本文标题:崩溃集锦

      本文链接:https://www.haomeiwen.com/subject/rtgxoxtx.html