模糊Swift中的低级致命错误

时间:2016-10-03 19:46:15

标签: swift

我有一个有序集的Swift实现,我认为它是正确的(如果效率不高),因为NSOrderedSet不能用于非ObjC结构。但是,在多线程应用程序中使用类时,我偶尔会遇到一个非常低级别的崩溃,它在控制台上没有报告任何内容但是“致命错误”,并在调试器中显示:

function signature specialization <preserving fragile attribute, Arg[1] =
[Closure Propagated : reabstraction thunk helper from @callee_owned
(@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to
@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) ->
(@out ()), Argument Types : [@callee_owned (@unowned
Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic
specialization <preserving fragile attribute, ()> of
Swift.StaticString.withUTF8Buffer <A>
((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A

正在执行的代码是:

import Foundation

fileprivate struct OrderedSetElementWrapper<T>: Hashable, Equatable where T: Hashable, T: Equatable {
    let value: T
    let index: Int
    var hashValue: Int { return self.value.hashValue }
}
fileprivate func ==<T>(lhs: OrderedSetElementWrapper<T>, rhs: OrderedSetElementWrapper<T>) -> Bool {
    return lhs.value == rhs.value
}
public struct OrderedSet<T> where T: Hashable, T: Equatable {
    private var _set = Set<OrderedSetElementWrapper<T>>()

    init() {}
    init(array: [T]) { array.forEach { self.insert($0) } }

    var array: [T] { 
        // ***** Error intermittently on next line ******
        let sortedArray = self._set.sorted { $0.index < $1.index }
        // **********************************************
        let mapped = sortedArray.map { $0.value }
        return mapped
        //return self._set.sorted(by: { $0.index < $1.index }).map({ $0.value }) 
    }
    var set: Set<T> { return Set(self._set.map { $0.value } ) }
    var count: Int { return self._set.count }

    func contains(item: T) -> Bool { return self.set.contains(item) }
    mutating func insert(_ item: T) {
        self._set.insert(OrderedSetElementWrapper(value: item, index: self._set.count))
    }
    mutating func remove(_ item: T) {
        var newSet = Set<OrderedSetElementWrapper<T>>()
        for element in self._set {
            if element.value != item {
                newSet.insert(OrderedSetElementWrapper(value: element.value, index: newSet.count))
            }
        }
        self._set = newSet
    }
}

这个类在绝大多数时候都能完美运行,我对逻辑很有把握,而我根本不理解错误信息!任何人都可以解释错误信息吗?

通过调用.array属性触发错误。调用类是

public class XXXItemGroupBase: XXXItemBase {
    // ***************************************
    // MARK: New properties
    // ***************************************
    private var _theItems = OrderedSet<XXXItemBase>()
    // _theItems is used as a private backing store.
    // Do NOT access it directly outside of the class -
    // use self.components instead...

    // ***************************************
    // MARK: Overridden properties
    // ***************************************
    override public var components: [XXXItemBase] { // The public face of self._theItems
        get { return self._theItems.array } // **** CRASH HERE ****
        set { self._theItems = OrderedSet(array: newValue) }
    }

0 个答案:

没有答案