Xcode/Swift 错误:由于信号导致命令失败:分段错误:11

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

Xcode/Swift error: Command failed due to signal: Segmentation fault: 11

iosxcodeswiftsingleton

提问by Peyman

I am trying to call a function of a singleton class with completion handler argument but I get "Command failed due to signal: Segmentation fault: 11" error. I am using Xcode 6.2 (6C101) and trying to build for iOS 8 on iPhone 6 simulator. Here is the singlton class:

我正在尝试使用完成处理程序参数调用单例类的函数,但出现“由于信号导致命令失败:分段错误:11”错误。我正在使用 Xcode 6.2 (6C101) 并尝试在 iPhone 6 模拟器上为 iOS 8 构建。这是单顿类:

public class ClientManager {

    public class var sharedInstance: ClientManager {
        struct Singleton {
            static let instance = ClientManager()
        }
        return Singleton.instance
    }

    private init() {
    }

    public func fetchServiceInfo(serviceName: String, completionHandler: (JSON?, NSError?) -> Void) {
        Alamofire.request(.GET, Router.ServiceInfo(serviceName)).responseJSON { (req, res, json, error) in
            completionHandler(JSON(json!), error)
        }
    }

}

And when I call fetchServiceInfofunction within a view controller, Xcode crashes (SourceKitService Crashed):

当我fetchServiceInfo在视图控制器中调用函数时,Xcode 崩溃(SourceKitService Crashed):

ClientManager.sharedInstance.fetchServiceInfo("default") { (json, error) in
    println(json)
}

However, if I call the same function within the initmethod of ClientManagerit works properly:

但是,如果我在它的init方法中调用相同的函数,ClientManager它可以正常工作:

private init() {
    self.fetchServiceInfo("default") { (json, error) in
        println(json)
    }
}

I am using Alamofireand SwiftyJSONlibraries.

我正在使用AlamofireSwiftyJSON库。

回答by Carlos García

?Is posible you are using SwiftyJSON as framework, cocoapods or git submodule? Look at https://github.com/SwiftyJSON/SwiftyJSON/issues/125Using SwiftyJSON.swift file into your project must work fine

?您是否可以使用 SwiftyJSON 作为框架、cocoapods 或 git 子模块?查看https://github.com/SwiftyJSON/SwiftyJSON/issues/125Using SwiftyJSON.swift file into your project must work fine

回答by Orlin Georgiev

As Carlos Garcia correctly pointed out, the problem is with compiling SwiftyJSON. In his provided link https://github.com/SwiftyJSON/SwiftyJSON/issues/125, check the solution by nunogoncalves. In a nutshell, you MUST use the closure JSON parameter at least once in the closure body. Here is what I did:

正如 Carlos Garcia 正确指出的那样,问题在于编译 SwiftyJSON。在他提供的链接https://github.com/SwiftyJSON/SwiftyJSON/issues/125 中,检查 nunogoncalves 的解决方案。简而言之,您必须在闭包主体中至少使用一次闭包 JSON 参数。这是我所做的:

NetworkManager.sharedInstance.loadOrderDetails(187, onComplete: { (json, errorMessage) -> () in
    json?["aaa"] //this useless line fixes the compiler crash

    //the rest of your code here...
});