swiftsetswift-playgroundxcode9.4

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


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.


Solution

  • 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.