Swift compiler segmentation fault when building

后端 未结 30 2271
南旧
南旧 2020-11-29 03:59

Adding a (convenient) computed height property to UIView in my UIViewExtension.swift file is causing the Swift compiler to segfault...

相关标签:
30条回答
  • 2020-11-29 05:04

    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:

    1. Create a new file called SegFaultDebugger.swift in your project.
    2. In this new file, define an extension to the class that's giving you problems.
    3. Move a group of methods from the main file to SegFaultDebugger.swift.
    4. 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 into SegFaultDebugger.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.
    0 讨论(0)
  • 2020-11-29 05:05

    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.

    enter image description here

    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.

    0 讨论(0)
  • 2020-11-29 05:05

    Like @Fjohn said, this was an issue related to unwrapping an optional for me (broke in both Xcode 7.0 beta 6 and Xcode 7). In my case, I was not unwrapping optional of the optional (what tipped me off was double ?? in the descriptor. Using if let solved the issue

    0 讨论(0)
  • 2020-11-29 05:05

    For me the following caused a segfault while type is an optional:

    switch type {
        case .aType:
            // Do Something
        default:
            break
    }
    

    and this solved it:

    switch type {
        case .Some(.aType):
            // Do Something
        default:
            break
    }
    
    0 讨论(0)
  • 2020-11-29 05:06

    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.

    0 讨论(0)
  • 2020-11-29 05:06

    You can also have this problem if you declare a condition with an unwrapped Bool as a property

    0 讨论(0)
提交回复
热议问题