【问题标题】:Fatal error: Only BidirectionalCollections can be advanced by a negative amount致命错误:只能将 BidirectionalCollections 推进负数
【发布时间】:2018-09-01 13:02:28
【问题描述】:

如何使用 Xcode Playground 将 Array 转换为 Set?我们试过了:

let a = Array(0 ..< 1000)
let s = Set(a)

这会在运行时产生:

致命错误:只能将 BidirectionalCollections 推进负数

Xcode 9.4 Playground 和 Xcode 10 beta 3 Playground 都出现问题。

【问题讨论】:

    标签: swift set swift-playground xcode9.4


    【解决方案1】:

    这已在 Xcode 10 beta 6 及更新版本中得到修复,因此我更新了解决方法以仅适用于较旧的 swift 版本。


    对于较旧的 Xcode 版本(如 Xcode 9.4),这可能是由于元素数量大于 100 造成的。

    解决方法found by Karoy Lorentey ,是自定义Set的playground描述:

    #if !swift(>=4.2)
    extension Set: CustomPlaygroundDisplayConvertible {
        public var playgroundDescription: Any {
            return description
        }
    }
    #endif
    
    let a = Array(0 ..< 1000)
    let s = Set(a)
    

    这样做不会在运行时出错。

    【讨论】:

    • 我在 Xcode 10b6 中没有看到这个问题。这可能是一个已修复的错误?
    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2017-10-30
    • 2018-03-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多