28

I just downloaded the GM release of Xcode 6 and it won't compile with this error:

Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

Any ideas on how to fix this?

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
bhzag
  • 2,932
  • 7
  • 23
  • 39
  • http://stackoverflow.com/questions/10373016/command-xcode-app-contents-developer-toolchains-xcodedefault-xctoolchain-usr-bi?rq=1 please see this one. solved my problem:) – Zidong Oct 05 '14 at 05:45
  • All common answer did not solved my problem, I figure out that issue was pretty different -http://stackoverflow.com/questions/25889723/swift-failed-with-exit-code-1-while-compiling-in-xcode-possibly-related-to-bri/27271734#27271734 – Kampai Dec 03 '14 at 12:28

21 Answers21

70

This error can happen for numerous reasons, so this is meant to be a debugging hint. You may want to try using xcodebuild in the command line. It will give you details as to what files are the culprits.

To do this, open Terminal and go to your project folder. Once there, type in

xcodebuild -project YourProject.xcodeproj -scheme YourScheme

or if you're working in a workspace

xcodebuild -workspace YourProject.xcworkspace -scheme YourScheme

You may see A LOT of messages pop up, but at the very end of the output you should see the specific files that are causing the crash. Back in XCode, go into those files and start playing with some of the Swift syntax to see what's going on. In my case, it had to do with the setAttributeString function, but I've seen other people have issues with ! and ?.

Hopefully that will get you headed in the right direction.

Maxwell
  • 6,532
  • 4
  • 37
  • 55
  • 1
    This helped me to find the real problem – iTukker Nov 20 '14 at 14:38
  • Just an addendum: In my case, this error was popping because I was sending nil for an optional String parameter. Using "" instead got rid of the error. – Joshua Kaden Feb 17 '15 at 17:09
  • 1
    Also found this to be super helpful. Should be the accepted answer! – Andy Poes Jun 19 '15 at 20:21
  • TryProjects/ContentOrganizer.swift:15:8: error: no such module 'Alamofire' import Alamofire ^ this is what i get. it does work in the simulator and my phone, but when i run or do a command line compile this is my problem. i tried removing alamofire and then it gets bothered by the next pod i imported. any ideas how to solve this? – Esqarrouth Nov 04 '15 at 13:50
  • 1
    @Esq I think this is because cocoapods does stuff alongside the `xcodeproj` file to successfully compile, which would be why it's not finding the Alamofire module. This worked for me: `xcodebuild -workspace MyProject.xcworkspace -scheme MyScheme` You need to tell it to compile your workspace instead of just the project – Chris Nov 25 '15 at 03:55
  • thank you so much, I was thinking I'll spend ages looking for the error reason in our huge project! – Anton Tyutin Nov 08 '16 at 20:12
10

I had to change my "Optimization Level" to None[-0none]

Target > Build Settings > Swift Compiler > Optimization Level.

Byron Coetsee
  • 3,533
  • 5
  • 20
  • 31
  • 2
    I would warn that there are two settings named "Optimization Level", make sure you're changing the one under Swift Compiler section. I lost a few hours solving my case. – nalexn Nov 30 '14 at 11:28
7

My case was a little bit different and it involves enums and optionals. For the simplicity lets define

enum Animal {
    case Dog
    case Cat
}

func exampleAction(animal: Animal) {}

exampleAction(.Cat)

It will run OK. However as soon as I made the argument optional the error started to appear. So this code won't work:

func exampleAction(animal: Animal?) {}

exampleAction(.Cat)

To make it work I had to add explicit enum name in the method call. So the following code worked again:

exampleAction(Animal.Cat)
Michał Kreft
  • 548
  • 3
  • 16
  • Just to add my two cents, for me it was on a normal class (and not enum) like: myButton.setTitleColor(.grayColor(), forState: .Normal) has to be changed to myButton.setTitleColor(UIColor.grayColor(), forState: .Normal) with the UIColor class. – Dam May 27 '15 at 08:28
3

I think it occured for many reasons, what I have encountered is this situation,hope it could help you.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ){ [weak self] in

        // ...
        dispatch_async(dispatch_get_main_queue()) { [weak self] in

            // ...
            return

        }
    }

In the upper code,just delete "[weak self]" called capture list will remove the compiler error. It works for me.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ){ [weak self] in

        // ...
        dispatch_async(dispatch_get_main_queue()) {

            // ...
            return

        }
    }

xCode version is 6.1.1

tounaobun
  • 14,570
  • 9
  • 53
  • 75
2

May be the same issue Swift compile error in XCode 6 GM

I had the same problem, then I use git checkout old versions to find which commit cause the problem, and try to find the key problem code. My key problem code is something like this

func afunc() {
    class AClass() {
    }
    let ac = AClass()
    //....
}

Other code could make the same problem, occur in Swift Optimization, and swift compiler doesn't tell you the exact location. This must be a bug by Apple.

Community
  • 1
  • 1
tuoxie007
  • 1,234
  • 12
  • 10
  • 1
    Actually going back and finding the difference made it. In my case it was a struct local to a func which made it crash. The compiler is nice but still it's a late beta :-( – qwerty_so Dec 30 '14 at 16:03
  • @ThomasKilian I had the same problem. Here is my code: `func base() -> Void { struct ColorStruct { var WHITE = "" init() { if let PP = delegate.PP { WHITE = PP.UI.Colors.WHITE } } } var COLOR = ColorStruct() }` – Andrew Mar 10 '15 at 14:07
  • 1
    @AndrewAnthonyGerst Swift/XCode really sucks. Astonishing how patient Apple users are. I'm sitting here cursing on an hourly basis... – qwerty_so Mar 10 '15 at 14:12
2

In my case i changed 3 places:

Target > Build Settings > Swift Compiler >

  • Optimization Level.
    • Debug: None [-Onone]
    • Distribution: Fastest [-O]
    • Release: Fastest [-O]

When i changed just Debug, i have errors like "Source Kit crashed...." This combination of parameters, works very good for me!

Weles
  • 1,275
  • 13
  • 17
2

If you are using some API which should be used in early iOS version, you may get build failure. For example: If you are using UI_USER_INTERFACE_IDIOM() instead of UIDevice.currentDevice().userInterfaceIdiom to identify the device type, you will get this build error with no hint.

evanchin
  • 2,028
  • 1
  • 22
  • 25
  • You xcodebuild in command line, you may get an error like this. "Global variable initializer type does not match global variable type! %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_" LLVM ERROR: Broken module found, compilation aborted!" – evanchin Apr 23 '15 at 11:03
2

A lot of people have this issue (me included) due to the optimisation on the compiler. I don't consider turning off the optimisation a correct resolution - I want my code optimised to run as quick as it can.

re-running xcodebuild manually didn't do any good as it ran it without the optimisations as well.

However - the error screen gave me the swiftc command that was failing:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -target arm64-apple-ios8.0 -incremental -module-name SpaceCats -O -sdk /Applic...

The -O there is the optimise flag.

I re-ran this whole command in the projects directory (as per xcodebuild recommendation above) and amongst all the details I found the error below:

{
"kind": "finished",
"name": "compile",
"pid": 10682,
"output": "Bitcast requires both operands to be pointer or neither\n  %228 = bitcast i8* %227 to %VSs20UnsafeMutablePointer, !dbg !1322\nBitcast requires both operands to be pointer or neither\n  %324 = bitcast i8* %323 to %VSs20UnsafeMutablePointer, !dbg !1322\nBitcast requires both operands to be pointer or neither\n  %411 = bitcast i8* %410 to %VSs20UnsafeMutablePointer, !dbg !1322\nBitcast requires both operands to be pointer or neither\n  %498 = bitcast i8* %497 to %VSs20UnsafeMutablePointer, !dbg !1322\nLLVM ERROR: Broken function found, compilation aborted!\n",
"exit-status": 1
}

If you want it to go to a file instead of the screen, add " 2>filename" to the end.

I then had to find the pid (10682) in the file to see what it was compiling. I ran the command for that pid manually and it gave me the error for the specific file. Then it comes down to fixing the code.

TPot
  • 340
  • 3
  • 10
  • Good answer, we had this problem trying to do a release build. Non release builds worked just fine (not optimized) then switching to release, swiftc failed as per the error. Wish you could see all that output in XCode too... – Kendall Helmstetter Gelner May 19 '15 at 22:16
  • I had been stuck for days with that swiftc compiler error, and this my friend, this brings me to life again. If one day you need a place to stay, you can crush in my couch as long as you want. Thanks. – Julio_oa Apr 17 '17 at 23:47
2

I see many reasons. My answer is not a generic solution but just adding yet another case that provide to that error. In my case it was setting a button's title like this:

button!.setTitleColor(.whiteColor(), forState: UIControlState.Normal)

instead of this:

button!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
Julian
  • 9,299
  • 5
  • 48
  • 65
1

In my case this error was caused by the incorrect (UTF8) .swift file encoding; Solved by copy-pasting the file's contents into a new file.

0

change

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  

TO

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

and do the same to others like this

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Wstunes
  • 196
  • 1
  • 9
  • 3
    What do you mean other functions like this? – bhzag Sep 11 '14 at 00:38
  • Many Foundation functions with optional values has been changed. Find them out and update them – Wstunes Sep 11 '14 at 01:11
  • 3
    I've removed all the forced unwrappers of optionals (all exclamation points) from function definitions and my colleague and I am still getting this error. Any other ideas? – Chuck Smith Sep 26 '14 at 08:36
0

Go to the folder: Users/user/Library/Developer/Xcode/DerivedData

and clear all file,

then then clean and analyze,

It`s work to me.

Harris Fu
  • 21
  • 6
0

I have the following code showing build error in Release.

if myValue > 5 { myValue = 5.0 }
if myValue < 0 { myValue = 0 }

Adding else between if statements fixed the issue:

if myValue > 5 { myValue = 5.0 }
else
if myValue < 0 { myValue = 0 }

Demo: https://github.com/exchangegroup/build-error-demo

Xcode Version 6.1 (6A1052d). Builds alright in debug. Submitted ticket to Apple Bug Reporter.

Evgenii
  • 36,389
  • 27
  • 134
  • 170
0

Just to put this out there: I get this error whenever I put [unowned self] in a block within a block, like this:

lazy var someVar: SomeType = {
    self.successBlock = {[unowned self] in
        // ...
    }
}()

The solution is to put the [unowned self] only at the topmost-level block. It seems an unowned reference to self is handled automatically for blocks within blocks.

Of course, I was only able to find this error by first finding out the troublesome file via @Maxwell's answer: https://stackoverflow.com/a/26848000/855680

Community
  • 1
  • 1
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
0

To add my case here. I got the described error whenever I mark the closure with [unowned self], but never reference the self in the closure itself.

For example:

        request.startWithSuccess({ [unowned self] (req: CBRequest!, object: AnyObject!) -> Void in

            if let result = object["result"] as? [NSObject: AnyObject]
            {
                popup.type = result["email"] == nil ? AuthPopupType.Signup : AuthPopupType.Login
            }
            else
            {
                println("WARNING: Malformed response for kCBCheckUniquenesPath request.")
            }

            }, failure: { (req: CBRequest!, err: NSError!) -> Void in

                println("ERROR: Failure response for kCBCheckUniquenesPath request.")
        })
0

By following @maxvel's suggestion, I got to know that max() and min() functions failed to compile in the release mode. I replaced it with my own functions like below.

//Swift compiler seems to failing to compile default min and max functions in release 
//mode hence writing my own
//
func maximum<T: Comparable> (one: T, other: T) -> T {
    if one > other {
        return one
    }
    else {
        return other
    }
}

func minimum<T: Comparable> (one: T, other: T) -> T {
    if one < other {
        return one
    }
    else {
        return other
    }
}
Raj
  • 1,091
  • 1
  • 9
  • 18
0

Maxwell's solution gives you the closest hint.

Akhlaq Rao
  • 29
  • 4
0

Encounter this error when compiling this Swift 2.0 syntax in Xcode 6.4:

print(string, appendNewline: true);

Back to Xcode 7 and the error is gone.

BillChan
  • 85
  • 1
  • 3
0

In my case Xcode gave the error because of the following line :

if UI_USER_INTERFACE_IDIOM() ==  UIUserInterfaceIdiom.Phone {


}else {

}

And to fix the error I've defined this :

enum UIUserInterfaceIdiom : Int {
    case Unspecified
    case Phone // iPhone and iPod touch style UI
    case Pad // iPad style UI
}

And then i used it like :

 if UIDevice.currentDevice().userInterfaceIdiom == .Phone {

}

Good luck !

AaoIi
  • 8,288
  • 6
  • 45
  • 87
0

I was also getting this error. I ran the the command in Terminal as suggested by @Maxwell and found out the error was in my GameViewController.swift file. A little digging around and found that it didn't like some auto-generated code or the code conflicted with a setting in Xcode somewhere

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}

As soon as I removed this block the error went away.

Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
-2

I think the real problem is there are too many errors, so the compile tell you only a confused error code.

But you can always open the every source code file, in there you can find the detail error information and correct advice.

Good luck!

Tinyfool
  • 1,460
  • 2
  • 18
  • 40