为什么这段代码不能在Xcode 8.2.1上编译

时间:2017-01-31 14:30:32

标签: swift3 xcode8.2

我正在尝试将完成块传递给Objective-C API,但Xcode表示由于信号导致构建失败:Segmentation Fault:11。

违规行似乎在browseAllFilesInRouter方法中:

let completion = { (response: Dictionary<AnyHashable, Any>?, error: NSError?) in
        print("response")
    } as? UPPResponseBlock

device.contentDirectoryService().browse(withObjectID: "0", browseFlag: BrowseDirectChildren, filter: "dc:title,upnp:originalTrackNumber,res,res@duration", startingIndex: 0, requestedCount: 0, sortCritera: nil, completion: completion!)

以下是更完整参考的完整课程:

import Foundation

class TVNetworkClient: NSObject, UPPDiscoveryDelegate {

    var routers: [UPPMediaServerDevice]
    var routerFileSystemTree: [UPPMediaItem]
    var searchTimer: Timer
    var completionBlock: ((_: [String: String], _: NSError) -> Void)?
    var notifyBlock: ((_: Bool, _: NSError) -> Void)?

    //MARK: - Initializers
    override init() {
        routers = []
        routerFileSystemTree = []
        searchTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) {
            _ in
            print("requesting data.")
            UPPDiscovery.sharedInstance().startBrowsing(forServices: "ssdp:all")
        }

        super.init()

        UPPDiscovery.sharedInstance().addBrowserObserver(self)
        searchTimer.fire()
    }

    convenience init(notify: @escaping (_: Bool, _: NSError) -> Void) {
        self.init()
        notifyBlock = notify
    }

    convenience init(completion: @escaping (_: [String: String], _: NSError) -> ()) {
        self.init()
        completionBlock = completion
    }

    //MARK: - Discovery Delegate
    func discovery(_ discovery: UPPDiscovery!, didFind device: UPPBasicDevice!) {
        if let server = device as? UPPMediaServerDevice {
            self.routers.append(server)
            self.browseAllFilesInRouter(device: server)
        }
    }

    func discovery(_ discovery: UPPDiscovery!, didRemove device: UPPBasicDevice!) {

    }

    //MARK: - Browse
    func browseAllFilesInRouter(device: UPPMediaServerDevice) {
        let completion = { (response: Dictionary<AnyHashable, Any>?, error: NSError?) in
            print("response")
        } as? UPPResponseBlock

        device.contentDirectoryService().browse(withObjectID: "0", browseFlag: BrowseDirectChildren, filter: "dc:title,upnp:originalTrackNumber,res,res@duration", startingIndex: 0, requestedCount: 0, sortCritera: nil, completion: completion!)

    }

}

我使用我正在使用的库创建了一个独立的xcode项目,因此您可以尝试一下。

https://drive.google.com/file/d/0By-7Jq4FK2KEcmRnS3lSVjdNcDA/view?usp=sharing

1 个答案:

答案 0 :(得分:0)

联系Apple并在Xcode 8.3 beta 2中修复它。

如果您仍然使用Xcode 8.2.1

,我找到了以下解决方法

当您使用尾随闭包语法直接在函数中传递闭包时,类型推断很有效:

device.contentDirectoryService().browse(withObjectID: "0", browseFlag: BrowseDirectChildren, filter: "", startingIndex: 0, requestedCount: 0, sortCritera: nil) { (response, error) in
        if let resp = response {
            print(resp)
        }
    }