Xcode“po”命令保留了价值

时间:2017-05-10 08:54:21

标签: swift xcode

我一直在调试我的代码,发现我的经理被去了解(这是我的错误原因 - 不是调用委托方法)。

奇怪的是,在调试过程中,我在设置了管理员委托(弱)之后使用了“po”命令,并且防止它被取消初始化(委托方法被调用)。

为什么?这是正确的行为吗?

Xcode 8.3,swift 3.1

编辑:

//a tap starts everything :)

    @IBAction func shareButtonPressed(_ sender: Any) {
        let requestManager = FacebookPostRouteRequest() //bug fixed by changing to instance variable
        requestManager.delegate = self
        requestManager.showShareBadgeDialog(self.badge!, onViewController: self)
    }

//in FacebookPostRouteRequest
        final weak var delegate: FacebookPostRouteRequestDelegate?

        func showShareBadgeDialog(_ badge: Badge, onViewController viewController: UIViewController) {
            let dialog = self.initDialog(onViewController: viewController)
            guard let imageURL = badge.imageURL else {
                self.delegate?.facebookPostRouteRequest(self, didCompleteWithResult: false)
                return
            }

            dialog.shareContent = self.generateImageShareContent(imageURL)
            self.show(dialog)
        }

 private func show(_ dialog: FBSDKShareDialog) {
        OperationQueue.main.addOperation {
            dialog.delegate = self //when printed out dialog.delegate delegate methods were called! Deinit of FacebookPostRouteRequest is not called.
            let showResult = dialog.show() 
            ...
            }
        }

extension FacebookPostRouteRequest: FBSDKSharingDelegate {
    func sharer(_ sharer: FBSDKSharing!, didCompleteWithResults results: [AnyHashable : Any]!) {
        ...
    }
        //other delegate methods implemented as well

}

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

@IBAction func shareButtonPressed(_ sender: Any) {
    let requestManager = FacebookPostRouteRequest()
    requestManager.delegate = self
    requestManager.showShareBadgeDialog(self.badge!, onViewController: self)
}

在最后一行之后,requestManager对象将被释放,因为它不再被引用,也不会调用任何委托方法。

使requestManager成为实例变量:

let requestManager = FacebookPostRouteRequest()
@IBAction func shareButtonPressed(_ sender: Any) {
    requestManager.delegate = self
    requestManager.showShareBadgeDialog(self.badge!, onViewController: self)
}

调试器的问题可能是停止主线程的竞争条件。