Compiler segmentation fault while using Set in Swi

2019-07-11 04:19发布

问题:

This code works fine:

let lines = ["one", "one", "two"]
let lineSet = lines

but while compiling this one:

let lines = ["one", "one", "two"]
let lineSet = Set(lines)

I'm getting:

Command failed due to signal: Segmentation fault: 11

I've got Xcode Version 9.0 (9A235). Is this is really a bug or am I just doing something wrong?

My workaround for now:

var lineSet = Set<String>()
let lines = ["one", "one", "two"]
lines.forEach { lineSet.insert($0) }

回答1:

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 ;)

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.



回答2:

Quoting John Caswell from here

A compiler segfault is always a bug

In my experience, Playgrounds is a bit buggy. XCode Playground gets stuck on "running playground" or "launching simulator", and then won't run the code. I get errors like this from time to time, and others have run into non-reproducible issues:

  • XCode hangs every time when i write few lines of code in playground
  • https://teamtreehouse.com/community/xcode-playground-gets-stuck-on-running-and-wont-run-the-code-what-to-do
  • https://forums.bignerdranch.com/t/playground-hangs-frequently/7331
  • https://forums.developer.apple.com/thread/75045
  • failed to launch stub for playground execution
  • Playground execution failed: error: Couldn't lookup symbols - Playground in xcode using swift
  • https://ericasadun.com/2015/03/16/swift-working-around-sources-playground-bugs/

(Although some of these links are old, Playground isn't that mature yet.)

Usually, the fix is to close Xcode, change your cursor's position, etc.

As a result, I either use https://repl.it/site/languages/swift, use Playground with caution, or use the latest Xcode (because that usually has the least amount of bugs).

EDIT:

Still when I'm using Xcode 9.4.1 both on my macbook air and macbook pro, Xcode crashes or just stops working on playground. Same thing for my colleague. Sometimes even when I'm just in Chrome the Playground just crashes!

I've been told that if you open Utilities and then toggle your platform from iOS to macOS then you may be able to resolve your Playground's freezing/hanging. But I haven't tried that.