在 URLSession 闭包中调用委托方法时崩溃
Crashes when calling delegate method inside URLSession closure
我创建了名为 FileTransferManager
的 class,它使用 URLSession 管理 upload/download 任务。
由于代码的长度,我创建了代码的要点。 : https://gist.github.com/Cyanide7523/eb2f7a743459055e13b8568a51f644f3
并且我创建了委托协议来识别传输结果。
这是 class 的示例用法:
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
调用委托函数时,应用程序总是崩溃并显示消息 "unrecognized selector sent to instance",我不明白为什么会崩溃。
+++ 错误日志
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 委托引用 "weak" 并在调用委托方法时删除所有强制解包(只需将“!”替换为“?”)。
我创建了名为 FileTransferManager
的 class,它使用 URLSession 管理 upload/download 任务。
由于代码的长度,我创建了代码的要点。 : https://gist.github.com/Cyanide7523/eb2f7a743459055e13b8568a51f644f3
并且我创建了委托协议来识别传输结果。
这是 class 的示例用法:
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
调用委托函数时,应用程序总是崩溃并显示消息 "unrecognized selector sent to instance",我不明白为什么会崩溃。
+++ 错误日志
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 委托引用 "weak" 并在调用委托方法时删除所有强制解包(只需将“!”替换为“?”)。