在 URL 闭包中调用委托方法时崩溃



我创建了名为FileTransferManager的类,它使用URLSession管理上传/下载任务。

由于代码的长度,我创建了代码的要点。 : https://gist.github.com/Cyanide7523/eb2f7a743459055e13b8568a51f644f3

我创建了委托协议来识别传输结果。

这是此类的示例用法:

class SampleViewController: UIViewController, FileTransferDelegate{
let fileMan = FileTransferManager()
fileMan.delegate = self
fileMan.download( /* Some parameters */ )
func fileTransferManager(_ sender: FileTransferManager, didSucceedDownload data: Data, ...) {
print("Download Succeed!")
}
}

但是当FileTransferManager调用委托函数时,应用程序总是崩溃并显示消息"无法识别的选择器发送到实例",我不知道为什么会崩溃。

+++ 错误日志

2018-06-27 14:31:57.851160+0900 Project[1428:2194695] -[Project.InitialViewController fileTransferManagerWithSender:willDownload:at:]: unrecognized selector sent to instance 0x10207a0e0
2018-06-27 14:31:57.851783+0900 Project[1428:2194695] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project.InitialViewController fileTransferManagerWithSender:willDownload:at:]: unrecognized selector sent to instance 0x10207a0e0'
*** First throw call stack:
(0x184d1ad8c 0x183ed45ec 0x184d28098 0x18ee0adb0 0x184d202d4 0x184c0641c 0x1003974b0 0x100399094 0x100396d8c 0x1852a9e4c 0x1852c2b6c 0x185742e88 0x1856848d0 0x185683cac 0x101ec119c 0x101ecd7cc 0x101ec119c 0x101ecd7cc 0x101ecd6b0 0x185744750 0x101ec119c 0x101ece454 0x101eccd44 0x101ed27c8 0x101ed2500 0x18493ffac 0x18493fb08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

您是否在InitialViewController中实现了@objc optional func fileTransferManager(_ sender: FileTransferManager, willDownload contentID: String, at room: String)方法?此外,将FileTransferManager委托引用设置为"弱",并在调用委托方法时删除所有强制解包(只需将"!"替换为"?"(。

最新更新