问题描述
此代码工作正常:
let lines = ["one", "one", "two"]
let lineSet = lines
但是在编译这个时:
let lines = ["one", "one", "two"]
let lineSet = Set(lines)
我得到:
由于信号导致命令失败:分段错误:11
我有 Xcode 版本 9.0 (9A235).这真的是一个错误还是我做错了什么?
I've got Xcode Version 9.0 (9A235). Is this is really a bug or am I just doing something wrong?
我现在的解决方法:
var lineSet = Set<String>()
let lines = ["one", "one", "two"]
lines.forEach { lineSet.insert($0) }
推荐答案
构建 lineSet
的更好、更惯用的方法很简单:
A better, more idiomatic way to construct your lineSet
is simply:
let lineSet: Set = ["one", "one", "two"]
希望这能修复您的编译器崩溃.不幸的是,我无法完全重现您的原始问题,因此我无法确定我的修复是否会有所不同 ;)
Hope this fixes your compiler crash. Unfortunately, I was unable to fully reproduce your original issue, so I can't really confirm my fix will be any different ;)
如前所述,删除您的 DerivedData
文件夹是个好主意(~/Library/Caches/com.apple.dt.Xcode
也可能有所帮助).当 Xcode 开始出现异常行为时,这是一种有点标准的做法.
As already mentioned, deleting your DerivedData
folder is a good idea (also ~/Library/Caches/com.apple.dt.Xcode
might help as well). A somewhat standard practice when Xcode starts behaving erratically.
这篇关于在 Swift 中使用 Set 时出现编译器分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!