swift failed with exit code 1 while compiling in Xcode - possibly related to Bridging-Headers
Asked Answered
S

26

45

I have an Obj-C Project I'm trying to migrate to Swift. I did succeed with various classes but recently ran into an issue I can't seem to make sense of. When I try to compile my current code base I get the following (SUPER UNHELPFUL ERROR MESSAGE)

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

enter image description here

My only assumption is its somehow related to my bridging-headers but Xcode isn't giving me enough information to figure out if this is actually true.

I'm using Cocoapods to add the CorePlot to my project. I'm trying to migrate the following class to Swift:

Obj-C Class (ScatterPlotContainer.h)

#import <Foundation/Foundation.h>

@class CPTScatterPlot;

@interface ScatterPlotContainer : NSObject
@property (nonatomic, strong) CPTScatterPlot *ahrsAlt;
@property (nonatomic, strong) CPTScatterPlot *calibration;
@property (nonatomic, strong) CPTScatterPlot *coreAlt;
@property (nonatomic, strong) CPTScatterPlot *pitch;
@property (nonatomic, strong) CPTScatterPlot *roll;
@property (nonatomic, strong) CPTScatterPlot *slip;
@end

Obj-c Class (ScatterPlotContainer.m)

#import <CorePlot/CPTScatterPlot.h>
#import "ScatterPlotContainer.h"


@implementation ScatterPlotContainer {

}
@end

Swift Conversion

import Foundation

 class ScatterPlotContainer : NSObject {
    public var ahrsAlt : CPTScatterPlot;
    public var calibration : CPTScatterPlot;
    public var coreAlt : CPTScatterPlot;
    public var pitch : CPTScatterPlot;
    public var roll : CPTScatterPlot;
    public var slip : CPTScatterPlot;
}

My bridging headers file

#import <CorePlot/CPTScatterPlot.h>

What I've tried thus far

When I comment out the #import <CorePlot/CPTScatterPlot.h> from the Bridging headers file - I get an error in swift because it doesn't know what CPTScatterPlot is

I've also tried #import <CPTScatterPlot.h> which didn't work either.

Thoughts

So the only thing I can think of is perhaps because I'm using a cocoa pod there is some sort of module name I need to add. The error message really isn't that useful. Does anybody have a suggestion about some blaring error I've made or how to get a more descriptive error message to figure out what is going on?

Shutin answered 17/9, 2014 at 11:46 Comment(1)
All Googlers: This error is a "catch all" that can have many reasons. Cleaning often helps. If not, retrace your steps and find out which changes cause the compiler to fail. Then, open a bug report.Swivel
E
44

I did the same all answer says but mine issue was not resolved. I did figured out that issue was related to broken function call.

A function syntax was not wrong but its calling mechanism was wrong.

To check the exact error for this issue check following:

Select issue navigator > Click on error will show logs for error > In that select All Messages tab.

This will show all detail logs for this error.

enter image description here

Scroll down and You got logs like, in my case

enter image description here

So, by reading this I figure out that something wrong with function calling. I browse my code and resolved it, Below was correct and wrong code.

Wrong Way:

var region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span)
// It will not shown error here but when you build project compiler shows error.

Right Way:

let region = MKCoordinateRegion(center: self.mapView.userLocation.coordinate, span: span)
Esposito answered 3/12, 2014 at 12:25 Comment(6)
Xcode 6 doesn't actually put in logs like that(scrolling down just shows the next steps), I ended up having to run xcodebuild thanks to this answer https://mcmap.net/q/15844/-gm-release-of-xcode-6-compileMenses
@DanielGalasko: The error above is obviously hard to find. I had put a one way that how I did solved it. There many different reason for above error.Esposito
I had a similar issue, with viewController.presentViewController(true, completion:nil) ... I was linking completion blocks with async networking calls (3 or 4 levels - a.k.a. callbkack hell) and apparently swift compiler didn't liked it. I avoided multiple levels on callback blocks as shown previously and it fixed my problem :-)Gujarati
@Kampai Thank you so much :)Desouza
Awesome .. this actually solved my problem.. Few files were missing from the folder structure and I added them back from the Finder. Thank you @KampaiArak
Thank you! In my case, when selecting "All Messages", I get the info that Xcode actually cannot found my bridging header, so I have to give the correct path to the bridging header, and problem solved.Mudlark
L
8

I run into this last night and nothing above was solving my problem. I was about to do something very bad at my laptop when I saw, all by pure luck, that ONE (1) file was is text encoding set to UTF-16 ?!?! WTF??

enter image description here

This was the last file I was working on, and probably, one bad cut/paste "import" a strange character into the arena. I did a cut/paste of my code in this file to a bare bone text editor. I deleted the file, recreate it and paste back my code... and voilà! it work.

So do the above, but also check your file encoding! :-)

Legionnaire answered 12/1, 2015 at 1:0 Comment(1)
Thanks this worked! Just to highlight, what you already said, for others: only changing the encoding had no effect for me, but after copying text away, deleting file, creating new file and pasting back in was the problem gone.Cyanite
U
4

I had the same error message. What helped, was to set the optimization level in the swift compiler settings to None. This is not really a solution for me and I think that's one of the many bugs in the swift compiler.

Uranalysis answered 30/9, 2014 at 18:18 Comment(3)
This also resolved the issue for me, though I'm trying to find specifically the code that the optimizer hates.Nadler
working for me too but still looking why the optimizer hates my code -_-Conjugated
I tried everything but I am still stuck with the error.Alcaic
I
2

Another solution for this issues is to check that you don't have 2 or more files with the same file names. It solved the problem for me.

Isocrates answered 13/8, 2015 at 23:0 Comment(0)
H
2

Thank you @Kampai for the advice on going through the error log message. I read through, and some files were missing:

<unknown>:0: error: no such file or directory:  

Somehow, some files were removed during a pull from GitHub. The files are in the directory, but not in the Xcode project.

Right click on a folder and click 'Add files to ...' to manually add missing files to Xcode. That fixed the problem for me.

This happened to me several times already, but now I know how to fix it \o/

Hippel answered 7/9, 2016 at 19:58 Comment(0)
C
1

I was getting the same error for including this code in a didSet block:

didSet {
  // Test whether this view is currently visible to the user.
  if super.isViewLoaded() && (super.view.window != nil) {
    // (build fails even if this block is empty)
  }
}

It took a lot of trial/error to hunt this down. Removing super. allowed the build to proceed.

Concubinage answered 15/10, 2014 at 17:27 Comment(2)
Similar to this, my case were calling super in set and get of overridden property. The workaround to this was to define a function that calls and returns the super value and to call this function from the custom getter and setter.Ostwald
Were you able to take-one-for-the-team and digest this down to a bug report? I had a similar case, but I could not extract it without the bug going away.Beck
E
1

had a horrible time with this bug for over 3 hours by meticulously going from file to file and reverting the changes and seeing if that file had the issue in it. I tried the first answer but didn't give me any answers. Found the issue and it was because I had a non computed property named the same as a computed property of a subclass. I really hope the debugger becomes more robust with handling these sorts of cases in future updates :(

Eclair answered 15/1, 2015 at 22:34 Comment(0)
E
1

Simply deleting derived data and cleaning helped me

Edd answered 28/3, 2017 at 15:3 Comment(0)
H
0

1) Identify the file there the problem is. You can copy and paste the compilation instruction to the console and the last screen will contain the error description. Note the pid number there the problem was identified. Then scroll up and find the pid and related instruction - there will be one file per pid, so you will find the file you have problem it.

2) Look through the file and check all you last changes. If you have git initialized you can use

git diff <file name>
Hindermost answered 13/1, 2015 at 10:40 Comment(1)
This answer sounds interesting as a way to identify the problem, but I can't follow it. An example would be really helpful!Eboat
B
0

Since everyone else has been showing theirs, I'll show mine:

class Foo : UIView {
    var pathPosition:Double = 0.0 { didSet {
        pathPosition = min(max(0.0, pathPosition), 1.0) // crashes if this line is present
        self.pathPosition = min(max(0.0, pathPosition), 1.0) // but not here
        }}
}

Incredibly, this does not come up in Playground, but does fail when placed in code in a framework. Although it is legal syntax (used to work, still works in playground), the Swift compiler seems to want pathPosition to be qualified with self.. Note that is (relatively) old code and used to compile, maybe something broke in 6.1.

Edit:

I feel like I am going insane, but it feels like there is a greater complexity problem going on here, where surrounding code can impact this problem. I saw things compile last night, changed some code and settings again today, and it failed on me again. Today, I had to hack in a bunch of really stupid code to get it to work:

var pathPosition:Double = 0.0 { didSet {

    // bug: insane!! - have to clobber the value before resetting!
    let bugOldValue = pathPosition
    self.pathPosition = 1.0 // fails without this nonsensical line!
    self.pathPosition = min(max(0.0, bugOldValue), 1.0)
}}

For what it's worth, the actual error message I got, per the helpful instructions above, was:

PHI node has multiple entries for the same basic block with different incoming values!
%14 = phi double [ 1.000000e+00, %10 ], [ %11, %10 ], [ 1.000000e+00, %9 ], [ 0.000000e+00, %9 ], !dbg !4818
label %10
double 1.000000e+00
%11 = phi double [ %7, %entry ], !dbg !4815
LLVM ERROR: Broken function found, compilation aborted!

I'm scared for tomorrow.

Beck answered 4/2, 2015 at 1:3 Comment(1)
I simply replaced min and max functions by my own and got rid of that mess.Jarmon
A
0

As for @Kampal, I'm still struggling to figure out how much to specify in a function call. For instance, creating a UIColor object sometimes requires that UIColor be specified, and sometimes doesn't.

These both work:

playButton.backgroundColor = .darkGrayColor()
playButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

This yields the exit code 1 error on compilation, without any debugger warning. #time-sucking debug vortex

playButton.setTitleColor(.whiteColor(), forState: UIControlState.Normal)  

So I have a new rule: when using a function that takes more than one parameter, be explicit.

Now back to playing swift: AVOID THE VORTEX

Alarcon answered 4/2, 2015 at 17:59 Comment(1)
At the very least, #time-sucking debug vortex made us laugh whilst we experienced this issue :)Greenish
S
0

I just had this same error, the problem was that I had overridden a method with a non-optional parameter and had made the parameter optional in the override. (the method parameter below)

func logNetworkCallDurationForMethod(method:String, path:String, milliseconds: UInt) {

}

override func logNetworkCallDurationForMethod(method:String?, path:String, milliseconds: UInt) {

}
Selfsacrifice answered 30/11, 2015 at 16:24 Comment(0)
B
0

Ran into this issue today actually. Was the result of a recent pull from git on a project where a file had been deleted, but it didn't update in my local project.

Path to missing file

Clicking on the error brought up the location of the "missing" file, went and deleted it's reference in the Project Navigator. Fixed the error, did a clean, and compiled successfully.

Bridgeboard answered 25/3, 2016 at 20:25 Comment(0)
G
0

This happened to me when trying to reference a method from an inmutable protocol argument(by mistake, I thought the member was a property):

Having an interface as follows:

public protocol NSValidatedUserInterfaceItem {
  func tag() -> Int
}

Compilation crash

func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool {
    print(anItem.tag) // oopsie, tag is a function
    return false
}

Compilation success

func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool {
  print(anItem.tag()) // this is cool for swift
  return false
}
Garbo answered 27/5, 2016 at 16:8 Comment(0)
D
0

This happened to me and after reading the log in issue navigator I found out that I have two swift files with same name. This was creating the issue and I was getting build failed.

Diamagnet answered 20/6, 2016 at 6:2 Comment(0)
B
0

I got this error due to a missing file in my project. Added this file again and voila everything worked.

Buckden answered 1/7, 2016 at 9:28 Comment(0)
T
0

In my case it was wrong method overriding. Base class:

open func send(_ onSuccess: @escaping ((SomeType) -> Void)) -> SomeType { }

Subclass:

open override func send(_ onSuccess: ((SomeType) -> Void)) -> SomeType { }

As you see @escaping is missing. Swift3 converter in XCode8 doesn't consider inheritance relations, moreover, that type mistakes aren't marked as errors.

Thigpen answered 20/9, 2016 at 13:0 Comment(0)
H
0

In My Case it was Simulator bug just uninstall app from simulator and clean project then run project.

Hagbut answered 4/1, 2017 at 13:55 Comment(0)
S
0

I had accidentally dragged symlinks (aliases) to source files into the project instead of the actual files.

Stator answered 16/3, 2017 at 19:46 Comment(0)
F
0

I had CoreData generated files twice (and added myself). Check the files are not duplicate.

Flynn answered 27/3, 2017 at 13:18 Comment(0)
P
0

Unfortunately this error is often caused by a glitch inside Swift's compiler. It is not always easy to find the reason. If cleaning doesn't work, my suggestion is to try to comment the last code you wrote (even the whole file if necessary). Usually commenting the last code you entered would restore the compilation and you'll get more meaningful errors. From there on you have to try to uncomment the code piece by piece until you get to the instruction which caused this error. The Swift compiler is still pretty young and from time to time it reports weird errors. These kind of errors are completely useless, because instead of helping the developers they only confuse them even more. I would suggest Apple to change the compiler to give more detailed information and avoid this annoying error from appearing anymore.

Pray answered 4/9, 2017 at 11:54 Comment(0)
A
0
NSString const *kGreenColor = @"#00C34E"; 

I had above line in my Constant.h file. which was meant for preprocessors only. Removing that line worked for me.

Annapurna answered 21/9, 2017 at 16:55 Comment(0)
A
0

moving the Bridge file to project level resolve my problem.

Aromaticity answered 2/10, 2017 at 6:5 Comment(0)
C
0

In my case I had renamed a file. After committing I found that the file name still hasn't changed in the Xcode project (not sure why), the file was greyed out. Changing the name and committing again did the trick.

So we have to keep an eye out for this error, when making changes to files using source control.

Concessionaire answered 28/3, 2018 at 7:54 Comment(0)
K
0

In my case deleted a couple of files directly from SourceTree but their reference was still there in Xcode. Logs show their names. Removed them and error went away.

Killiecrankie answered 13/9, 2020 at 9:40 Comment(0)
C
0

I had this error in flutter and I hadn't touched anything, so I gave

flutter pub cache clean

and reinstalled the packages and it worked

Chittagong answered 10/6, 2023 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.