gpt4 book ai didi

swift - Xcode "po"命令保留值

转载 作者:行者123 更新时间:2023-11-28 08:10:37 27 4
gpt4 key购买 nike

我一直在调试我的代码,发现我的管理器被取消初始化(这是我的错误的原因 - 没有调用委托(delegate)方法)。

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

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

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

}

最佳答案

你的问题在这里:

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

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

使requestManager成为一个实例变量:

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

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

关于swift - Xcode "po"命令保留值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43887760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com