【问题标题】:NSNotification breaking in Swift when trying to pass back userInfoNSNotification 在尝试传回 userInfo 时中断 Swift
【发布时间】:2015-03-10 19:18:33
【问题描述】:

我在尝试通过 Swift 中的 postNotification 传回 userInfo 时遇到问题。奇怪的是,代码仅在我尝试使用 userInfo 时才会中断——如果我将没有数据的通知发布到没有参数的选择器,一切正常。但是当我尝试传回 userInfo 时,我收到“无法识别的选择器发送到实例”错误。所以我的 ViewController 中的选择器签名有问题,但我不知道是什么。

这是中断的代码:

在我的表格视图中

let data = ["selection": selectedOption]
dismissViewControllerAnimated(true, completion: {NSNotificationCenter.defaultCenter().postNotificationName(wordsetPickedNotification, object: self, userInfo: data)})

在我的视图控制器中:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "wordsetPicked", name: wordsetPickedNotification, object: nil)
...
func wordsetPicked(n:NSNotification) {
    //do stuff
}

【问题讨论】:

    标签: ios swift notifications notificationcenter


    【解决方案1】:

    selector: "wordsetPicked" 更改为selector: "wordsetPicked:"

    【讨论】:

    • 非常感谢,这就像一个魅力......不敢相信我因为冒号而被困在这个上面一个多小时!
    • @DerrickHunt 很高兴它成功了!如果这完全解决了您的问题,请单击我的答案左侧的复选框,以便其他用户知道您的问题已解决。
    • 这里解释了为什么需要冒号 - stackoverflow.com/questions/24007650/selector-in-swift/…
    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多