Jakub
Jakub

Reputation: 2141

Compiler segmentation fault while using Set in Swift

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

Upvotes: 2

Views: 693

Answers (2)

mfaani
mfaani

Reputation: 36287

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:

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

Upvotes: -6

Paulo Mattos
Paulo Mattos

Reputation: 19339

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.

Upvotes: 0

Related Questions