Fatal error: Only BidirectionalCollections can be advanced by a negative amount

故事扮演 提交于 2019-12-13 03:14:43

问题


How to convert an Array to a Set using Xcode Playground? We tried:

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

This produces at run time:

Fatal error: Only BidirectionalCollections can be advanced by a negative amount

Issue happening with both Xcode 9.4 Playground and Xcode 10 beta 3 Playground.


回答1:


This is fixed in Xcode 10 beta 6 and newer, so I've updated the workaround to only apply for older swift versions.


For older Xcode versions (like Xcode 9.4), this may be caused by the number of elements being greater than 100.

Workaround found by Karoy Lorentey , is to customize Set's playground description:

#if !swift(>=4.2)
extension Set: CustomPlaygroundDisplayConvertible {
    public var playgroundDescription: Any {
        return description
    }
}
#endif

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

Doing like that will not have error at run time.



来源:https://stackoverflow.com/questions/52128359/fatal-error-only-bidirectionalcollections-can-be-advanced-by-a-negative-amount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!