致命错误:只有 BidirectionalCollections 可以提前一个负数 [英] Fatal error: Only BidirectionalCollections can be advanced by a negative amount

查看:45
本文介绍了致命错误:只有 BidirectionalCollections 可以提前一个负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Xcode Playground 将数组转换为集合?我们尝试过:

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

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

这在运行时产生:

致命错误:只有 BidirectionalCollections 可以提前一个负数

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

Xcode 9.4 Playground 和 Xcode 10 beta 3 Playground 均出现问题.

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

推荐答案

此问题已在 Xcode 10 beta 6 及更高版本中修复,因此我已将解决方法更新为仅适用于较旧的 swift 版本.

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

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

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

解决方法 由 Karoy Lorentey 发现,是自定义Set的playground描述:

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.

这篇关于致命错误:只有 BidirectionalCollections 可以提前一个负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆