【发布时间】:2015-09-05 17:04:17
【问题描述】:
我正在尝试使用 NSNotification 在两个 swift 类之间进行通信。我不知道我做错了什么,但是其他通知在哪里正常工作,其中一个不断将无法识别的选择器随机发送到实例异常。随机我的意思是每次我执行该代码异常是相同的,但类引用是不同的,如 __CALayer、__NSArray、__NSSet 等,我什至不直接使用这些类。有什么帮助吗?
这里是观察者类的初始化方法:
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "downloadChapter:", name: "downloadListNotification", object: DisplayMangaViewController.self)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "downloadChapter:", name: "downloadListNotification", object: DownloadRequestListViewController.self)
}
以及我在里面发布通知的那个:
let userInfo = ["downloadList" : self.selectedChapters , "mangaName" : self.obtainedMangaName]
let notification = NSNotification(name: "downloadListNotification", object: DownloadRequestListViewController.self, userInfo: userInfo as [NSObject : AnyObject])
NSNotificationCenter.defaultCenter().postNotification(notification)
这是一个例外示例:
2015-09-05 19:49:45.598 TurkİşManga[12708:58814] -[__NSArrayM downloadChapter:]: 无法识别的选择器发送到实例 0x7fbf9c80dd90 2015-09-05 19:49:45.600 土耳其漫画[12708:58814] *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[__NSArrayM 下载章节:]: 无法识别的选择器发送到实例 0x7fbf9c80dd90'
这里也是downloadChapter方法的草稿:
func downloadChapter(notification : NSNotification){}
【问题讨论】:
-
你需要发布你看到的错误,拜托。也是“downloadChapter:”函数的第一行或第二行。
-
我已经编辑过了,你可以检查一下吗?
-
在我的测试项目中,我无法得到您的错误。我会查看 userInfo 的内容。通过将其设置为已知值进行测试,例如: let userInfo = ["downloadList" : ["1","2","3"] , "mangaName" : ["a","b"]] 然后看看你是否可以这样追踪。
-
顺便说一下观察者类直接继承自NSObject,而发送者类继承自uiviewcontroller。不过这没关系,不是吗?
-
你确定观察者类在收到消息时还活着(还没有被释放)?
标签: ios swift notifications nsnotificationcenter