ios - append 到数组导致崩溃

标签 ios arrays swift append crashlytics

对于这段代码,我有以下崩溃报告。我不确定为什么这是错误的。

崩溃:

Crashed: com.apple.root.background-qos
0  libswiftCore.dylib                 0x1082ebf94 specialized _fatalErrorMessage(_:_:file:line:flags:) + 106516
1  libswiftCore.dylib                 0x10818c214 default argument 0 of _preconditionFailure(_:file:line:) + 109688
2  libswiftSwiftOnoneSupport.dylib    0x108804d60 specialized _ArrayBufferProtocol._arrayOutOfPlaceUpdate<A>(_:_:_:_:) + 55124
3  libswiftSwiftOnoneSupport.dylib    0x1087f9178 specialized Array._copyToNewBuffer(oldCount:) + 7020
4  libswiftSwiftOnoneSupport.dylib    0x1087f51bc specialized Array.append(_:) + 17480   

代码:

self.missingEmployees.removeAll()
for eid in eids {
    let indexFound = self.employees.index(where: {$0.employeeId == eid})
    if indexFound == nil {
        self.missingEmployees.append(eid)
    }
}

守卫会让 for 循环中的方法解决这个问题吗?

最佳答案

我自己刚刚收到这个错误。对我来说,这是由不同线程修改数组引起的。数组不是线程安全的。

我使用 GCD 解决了这个问题,并将追加操作放在同步块(synchronized block)中。在另一个地方,我使用 NSCache 解决了它,因为 NSCache 已经是线程安全的了。

关于ios - append 到数组导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51513884/

相关文章:

iphone - iOS 6 中的 ALAssetsLibrary 和位置服务

c - 类型、表达式和数组维度

ios - 尝试使用 SnapKit 在自定义 UITableViewCell 中布局 UILabels 时收到警告

iOS - 如何翻转 UIButton 的标题?

ios - 通过 Storyboard 自定义键盘高度

php - 将数组插入 mysql (userCake)

C++ 连接两个数组而不进行深度复制

ios - 正则表达式获取两个 % 字符之间的字符串

Swift 中的 UITableView

ios - 自定义UITableviewCell没有outlet