Swift compiler segmentation fault when building
Solution 1:
I had this error because I was doing this :
if(currentMeal?.State == .Deleted){
}
instead of
if(currentMeal!.State == .Deleted){
}
so I think optional not unwrapped in if condition can cause this error
Solution 2:
When you run into a compiler segfault in Swift, you don't get a handy line number and error message. Here's how you can track the problem down:
- Create a new file called
SegFaultDebugger.swift
in your project. - In this new file, define an extension to the class that's giving you problems.
- Move a group of methods from the main file to
SegFaultDebugger.swift
. - Compile.
At this point, one of three things happens:
-
You still get the segfault in the original file: Move the methods from
SegFaultDebugger.swift
back to the original file and move a different set of methods intoSegFaultDebugger.swift
. Repeat -
You get a segfault in
SegFaultDebugger.swift
: Great! Now use binary search to pin the segfault down to a specific method until you can figure out what construct is causing it. - You get meaningful compiler errors: Great! Fix the errors. Once everything compiles, move your methods back into the original file.
Solution 3:
I got this error while extending one of my protocols and mistyped and optional type argument.
protocol SomeProtocolName: class {
var someProtocolVariable: String { get set }
func someProtocolFunction(someProtocolVariable: String)
}
// MARK:
extension SomeProtocolName {
func someProtocolFunction(someProtocolVariable: String?) {
self.someProtocolVariable = someProtocolVariable
}
}
The difference in function arguments String
in prototype and String?
in extension caused Segmentation Fault 11.
Solution 4:
In Xcode 7, you can click on the error in the Debug Navigator and you'll be shown an expanded view of the crashes. Clicking on the hamburger button on the right expands the error, and if you scroll all the way down to the bottom of the expanded error message, you will see where it comes from.
For me, I had two of those segmentation fault errors. In the picture above, the first one is what it looks like when collapsed, the second is when you expand the hamburger button. At the very bottom of the expanded gray box, you'll see a message that says where the compiler crashed.
Note however that the error message may at times be not informative enough, so while it tells you where it crashed, it doesn't always say why and how to fix it. Getting rid of this error is still very much a matter of guesswork.