ios 在 Xcode 中编译时 swift 失败,退出代码为 1 - 可能与 Bridging-Headers 有关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25889723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:31:48  来源:igfitidea点击:

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

iosobjective-cswiftcore-plot

提问by Jeef

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)

我有一个 Obj-C 项目,我正在尝试迁移到 Swift。我确实在各种课程中取得了成功,但最近遇到了一个我似乎无法理解的问题。当我尝试编译我当前的代码库时,我得到以下信息(超级无用的错误消息)

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

命令 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc 失败,退出代码为 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.

我唯一的假设是它与我的桥接头有某种关系,但 Xcode 没有给我足够的信息来确定这是否真的是真的。

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

我正在使用Cocoapods将其添加CorePlot到我的项目中。我正在尝试将以下类迁移到 Swift:

Obj-C Class (ScatterPlotContainer.h)

Obj-C 类 (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)

Obj-c 类 (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

当我#import <CorePlot/CPTScatterPlot.h>从 Bridging 头文件中注释掉

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

我也试过#import <CPTScatterPlot.h>哪个也没用。

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?

所以我唯一能想到的可能是因为我使用的是可可豆荚,所以我需要添加某种模块名称。错误信息真的没有那么有用。有没有人对我犯的一些严重错误有什么建议,或者如何获得更具描述性的错误消息来弄清楚发生了什么?

采纳答案by Kampai

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)

回答by Steve S.

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??

我昨晚遇到了这个问题,上面没有解决我的问题。当我看到一个 (1) 文件是文本编码设置为 UTF-16 时,我正准备在我的笔记本电脑上做一些非常糟糕的事情,全靠运气?!?!跆拳道??

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! :-)

所以做上面的,还要检查你的文件编码!:-)

回答by heiko

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.

我有同样的错误信息。有帮助的是将 swift 编译器设置中的优化级别设置为无。这对我来说并不是真正的解决方案,我认为这是 swift 编译器中的众多错误之一。

回答by Fawkes

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.

此问题的另一个解决方案是检查您是否没有 2 个或更多文件名相同的文件。它为我解决了这个问题。

回答by AVog

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

感谢@Kampai 提供有关查看错误日志消息的建议。我通读了一遍,有些文件丢失了:

<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.

不知何故,一些文件在从 GitHub 拉取时被删除了。这些文件在目录中,但不在 Xcode 项目中。

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

右键单击文件夹并单击“将文件添加到...”以手动将丢失的文件添加到 Xcode。那为我解决了问题。

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

这已经发生在我身上好几次了,但现在我知道如何解决它\o/

回答by Andrey Chernukha

Simply deleting derived data and cleaning helped me

简单地删除派生数据和清理对我有帮助

回答by macu

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

将此代码包含在 didSet 块中时,我遇到了同样的错误:

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.

追捕它需要大量的试验/错误。删除super.允许构建继续进行。

回答by ImpactZero

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 :(

通过精心地从一个文件到另一个文件并恢复更改并查看该文件是否存在问题,在这个错误上度过了 3 个多小时的可怕时光。我尝试了第一个答案,但没有给我任何答案。发现了问题,这是因为我有一个非计算属性,其名称与子类的计算属性相同。我真的希望调试器在将来的更新中处理这些情况时变得更加健壮:(

回答by Chris Conover

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.

令人难以置信的是,这并没有出现在 Playground 中,但是当放在框架中的代码中时确实会失败。虽然它是合法的语法(曾经工作,仍然在操场上工作),但 Swift 编译器似乎希望 pathPosition 被限定为self.. 请注意,这是(相对)旧代码并用于编译,可能在 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.

我很害怕明天。

回答by Suz

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.

至于@Kampal,我仍在努力弄清楚在函数调用中指定多少。例如,创建 UIColor 对象有时需要指定 UIColor,有时则不需要。

These both work:

这些都有效:

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

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

这会exit code 1在编译时产生错误,没有任何调试器警告。#time-sucking 调试漩涡

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

现在回来玩 swift: AVOID THE VORTEX