1

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:

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 fetchServiceInfo function within a view controller, Xcode crashes (SourceKitService Crashed):

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

However, if I call the same function within the init method of ClientManager it works properly:

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

I am using Alamofire and SwiftyJSON libraries.

Peyman
  • 83
  • 2
  • 8
  • 2
    Are you by chance using the SwiftyJSON framework? Meaning not just taking a copy of the file. I've been unable to use the JSON object as a parameter when I use the framework. If I make a copy of the file and add it into my project though it seems to work fine. – Lucas Derraugh Jan 11 '15 at 22:25
  • 1
    Yes! I had it as framework before, but now I have it as a swift file in my project and it works fine. – Peyman Jan 12 '15 at 09:39

2 Answers2

1

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

Carlos García
  • 1,719
  • 1
  • 13
  • 6
0

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:

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

    //the rest of your code here...
});
Orlin Georgiev
  • 1,391
  • 16
  • 18