<分区>
在下面的例子中,数组 objectAtIndex:0 崩溃了,但是 firstObject 产生了一个 nil 值。为什么会这样?
NSArray *array = [NSArray new];
id obj1 = [array firstObject]; // this is giving nil value
id obj2 = [array objectAtIndex:0]; // this line crashes
<分区>
在下面的例子中,数组 objectAtIndex:0 崩溃了,但是 firstObject 产生了一个 nil 值。为什么会这样?
NSArray *array = [NSArray new];
id obj1 = [array firstObject]; // this is giving nil value
id obj2 = [array objectAtIndex:0]; // this line crashes
最佳答案
使用 firstObject 是系统定义的 API,如果没有则返回 nil。但是,如果您尝试访问 objectAtIndex,它将给出 NSRange 异常并且应用程序将崩溃。
关于ios - NSArray firstObject 和 objectAtIndex 的区别 :0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37045858/
相关文章:
ios - 如何仅对内容单元格而不是 UICollectionView 中的标题应用不同的 contentInset
ios - HDR成像opencv 3.1和swift 4.2图像旋转90度
ios - 尝试构建 DisplayCandy 时,终端给我这个错误
ios - 使用 NSPredicate 在类似于 SQL WHERE IN 子句的 NSArray 中查找元素
Objective-C 对象 typedef 产生奇怪的 @encode 并破坏 KVC
objective-c - ios中应用程序内按钮的气泡通知