如果在for in 循環里面,對數組進行修改,無論是增刪改 都會扔出一個異常。
如果時for循環的話就沒有問題
for in實際上是快速枚舉,enumerateObjectsWithOptions
//使用方法。
[arr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"idx=%ld, id=%@", idx, obj); //當需要結束循環的時候,調用stop,賦予YES
if (idx ==3) {
*stop = YES;
}
}];
同理 遍歷字典的用法
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
NSLog(@"value for key %@ is %@ ", key, value);
if ([@"key2" isEqualToString:key]) {
*stop = YES;
}
}];