xcode 8“由于信号导致命令失败:分段错误:11”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39748770/
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-09-15 09:22:01  来源:igfitidea点击:

xcode 8 "Command failed due to signal: Segmentation fault: 11"

swiftxcodeparsing

提问by stephen D

I updated my Xcode to Xcode 8 and accepted all the updates but I get an error telling me "Command failed due to signal: Segmentation fault: 11"

我将我的 Xcode 更新到 Xcode 8 并接受了所有更新,但我收到一条错误消息 "Command failed due to signal: Segmentation fault: 11"

note that I am aware this question is very close to this one :

请注意,我知道这个问题与这个问题非常接近:

Xcode 7 compile error : "Command failed due to signal: Segmentation fault: 11"

Xcode 7 编译错误:“由于信号导致命令失败:分段错误:11”

also note I am using a parse server, I doubt it has anything to do with it but I thought I will mention it in case.

还要注意我正在使用解析服务器,我怀疑它与它有什么关系,但我想我会提到它以防万一。

but it hasn't solved my issue, has anyone had this issue / solved it ?

但它并没有解决我的问题,有没有人遇到过这个问题/解决了它?

thanks !

谢谢 !

回答by mbachm

We encountered the same problem. This answer did solve our problem: Swift compiler segmentation fault when building

我们遇到了同样的问题。这个答案确实解决了我们的问题: 构建时的 Swift 编译器分段错误

You have to unwrap all optionals before you can use them in a if statement.

您必须先解开所有可选项,然后才能在 if 语句中使用它们。

回答by Gursimran Singh

This is a dynamic issue with Xcode but can be solved by modifying your own code in some way. It happened to me while I was migrating code from Swift2.3 to Swift3.1

这是 Xcode 的一个动态问题,但可以通过以某种方式修改您自己的代码来解决。当我将代码从 Swift2.3 迁移到 Swift3.1 时发生在我身上

In my case the error showed up at a method definition and the method is attached as an @IBAction for a UIButton.

在我的情况下,错误出现在方法定义中,并且该方法作为 UIButton 的 @IBAction 附加。

@IBAction func changeButtonTapped(_ sender: AnyObject) {
    // some code
}

Changing the above code to following solved the seg-fault issue for me.

将上面的代码更改为以下代码为我解决了段错误问题。

@IBAction func changeButtonTapped(_ sender: Any) {
    // some code
}


EDIT1: Another case of this segmentation fault came up.

EDIT1:这个分段错误的另一种情况出现了。

This time it was due to redeclaring a variable in higher scope and using in the same if statement Example code:

这次是由于在更高范围内重新声明了一个变量并在相同的 if 语句中使用了示例代码:

// 'var1' is defined in higher scope
func someFunction() {
    if let var2 = var1,
       let var1 = someValue {
        // some code
    }
}

Xcode got confused as to which 'var1' named variable to use to define var2. Changing names to something else will solve the seg-fault.

Xcode 对于使用哪个“var1”命名变量来定义 var2 感到困惑。将名称更改为其他名称将解决段错误。

回答by mm282

Most likely cause is a coding error that XCode hasn't picked up because it's built-in analysis has crashed/isn't keeping up. Simply restarting XCode should show you where the error is in your code.

最可能的原因是 XCode 没有发现的编码错误,因为它的内置分析已经崩溃/没有跟上。只需重新启动 XCode 即可显示代码中的错误位置。

回答by m_katsifarakis

In my case the error happens with code similar to this:

在我的情况下,错误发生在与此类似的代码中:

class AClass {
    var url: String?

    func aMethod() {
        guard let urlString = url, let url = URL(string: urlString) else {
            // Use `url`
        }
    }
}

Cause:

原因:

The fact that urlis an optional instance varthat I unwrap into itself, seems to crash the Swift compiler.

url是一个可选的实例变量,我将其解包到自身中,这一事实似乎使 Swift 编译器崩溃。

Locating the problem:

定位问题:

What's interesting and might give you a hint as to where the offending code is located, is that (at least in my case) the Xcode editor also crasheswhile you are writing the code:

有趣的是,可能会提示您有问题的代码所在的位置,是(至少在我的情况下)Xcode 编辑器在您编写代码时也会崩溃

enter image description here

在此处输入图片说明

Solution:

解决方案:

In my case, I just had to unwrap the optional instance varin another variable instead of itself (which was stupid in the first place...). E.g.:

就我而言,我只需要将可选实例解包到var另一个变量而不是它自身中(这首先是愚蠢的......)。例如:

class AClass {
    var url: String?

    func aMethod() {
        guard let urlString = url, let actualUrl = URL(string: urlString) else {
            // Use `actualUrl`
        }
    }
}

回答by Moennig

code with this error

有这个错误的代码

let fileManager = FileManager.default
let fileAttributes = try! fileManager.attributesOfItem(atPath: OSWConfig.documentsPath.appendingPathComponent(folderName) as String) as NSDictionary

//below code cause the error

let createDate = fileAttributes.object(forKey: FileAttributeKey.creationDate)! as! NSDate

Correct code with no error

正确无误的代码

let keysList = fileAttributes.allKeys as! [FileAttributeKey]

let valuesList = fileAttributes.allValues

let indexCreateDate = keysList.index(of: FileAttributeKey.creationDate)! as Int

var createDate: NSDate!
if indexCreateDate >= 0 {
    createDate = valuesList[indexCreateDate] as! NSDate

}

This solved my issue.

这解决了我的问题。