Xcode swift failed with exit code 254

2020-06-01 07:57发布

问题:

I am getting this compiler error in my code and I can't figure out why:

<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

The error is showing up somewhere in the following code segment:

var animalViewToSwap: AnimalView = animalViewMatrix.objectAtRow(0, andColumn: 0) as AnimalView
var currentRow = 0
var currentColumn = 0
var animalToSwapWith = true

var currentLocation = animalViewMatrix.findLocationOfObject(animalView)

currentRow = Int(currentLocation.row) - 1
currentColumn = Int(currentLocation.column) - 1

var rowDisplacement = 0
var columnDisplacement = 0

switch inDirection{

    case "left":
        columnDisplacement = withDistance * -1
        if (Int(animalViewMatrix.columns) > currentColumn + columnDisplacement)&&(currentColumn + columnDisplacement >= 0)&&(animalViewMatrix.objectAtRow(CInt(currentRow), andColumn: CInt(currentColumn + columnDisplacement)) is AnimalView)
        {
            animalToSwapWith = true;
        }
        else { animalToSwapWith = false }

    default:
        println("error")
        animalToSwapWith = false
        break
}

(I have more cases that are very similar and am leaving them out for simplicity - the bug isn't in them)

First Error

One bug is in the line: animalToSwapWith = falseand if I set it to true and comment all the rest out besides the variable initialization lines the error goes away. Also if I comment all of it out but instantiate animalToSwapWith to false the error occurs even though it doesn't when it is instantiated to true.

Second Error

There is a second error in the line:if (Int(animalViewMatrix.columns) > currentColumn + columnDisplacement)&&(currentColumn + columnDisplacement >= 0)&&(animalViewMatrix.objectAtRow(CInt(currentRow), andColumn: CInt(currentColumn + columnDisplacement)) is AnimalView) In this line all of these methods have been called earlier in the file with variables of the same types above so knowledge of the methods shouldn't matter.

Conclusion

Is there a reason why these two errors are occurring or is it because swift and Xcode-6 are still in beta testing and it is a bug in Xcode? Also note that when commenting the two errors out from each other one at a time the error message is the same.

回答1:

This is a Swift compiler bug, apparently testing for two or more implicitly unwrapped optionals causes the compiler to crash under some/many circumstances. Use Apple's Bugreporter to file this issue, mark it as duplicate of rdar://17212295.

Example

Here's a minimal example that crashes with the same error:

let a: String!
let b: String!

if a && b {
    println("have both")
}

Compile on command line as follows and witness the same crash:

$ xcrun swift -v -g crash.swift


回答2:

I was getting the same error and I tracked it down to this: I was extending NSError and in the extension was defining an enum. Moving the enum definition out of the extension fixed the error.

extension NSError {
    enum WYBErrorCodes: Int {
        case Fatal = 1000
        case XmlParse = 1100
        case CoreData = 1200
    }

    [...]
}


回答3:

I'm getting the same error when adopt NSTextViewDelegate protocol for my class. If I remove that protocol, compilation goes fine. Strange indeed.



回答4:

For the sake of providing other possible causes and how to fix them; I was trying to set the region of a map view within in dispatch block. If I commented out the setting of the region, the error goes away.

    dispatch_once(&centerMapLocation, {
//                var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
//                var theRegion: MKCoordinateRegion = MKCoordinateRegionMake(manager.location.coordinate, theSpan)
//                self.map.setRegion(theRegion, animated: true)
                })
        }


回答5:

I am getting this same error when I try to extend NSArray to have a foreach method:

extension NSArray {

    func foreach(f: (AnyObject -> ())) {
        for elem in self {
            f(elem)
        }
    }
}

It would appear that extending NSArray with func that takes a function argument will cause the same problems. I worked around the issue by defining a forEachInArray function that takes the NSArray as an argument:

func forEachInArray<T>(array: NSArray, f: (AnyObject -> ())) {
    for elem in array {
        f(elem)
    }
}


回答6:

In my case, i was calling an objective-c function from swift through bridge. Signature was like -

    - (SomeReturnType *)getSomething:(SomeOptions *)options
                          success:(void (^)(NSArray *response))success
                          failure:(void (^)(NSError *error))failure;

From swift, i was calling it as follows and getting compile error as "Xcode swift failed with exit code 254" -

    ObjCClass().getSomething(nil, success: {(response : Array!) in


        }, failure: {(error: NSError!)  in

        })

changing it to following worked for me -

     ObjCClass().getSomething(nil, success: {(response : [AnyObject]!) in


        }, failure: {(error: NSError!)  in

        })


回答7:

I had this error and the bug was solved in Beta 7 available online today.