【发布时间】:2017-01-19 17:39:48
【问题描述】:
我遇到了一个小问题。
假设我有结构。
struct AlertNotificationObject {
let message: String
}
我在 Swift 2.3 中有一个项目,我使用这个 structure 放入 dictionary,然后放入 NSNotification Observer 中的 userInfo:。我无法让它工作,所以为了调查我的问题,我创建了一些游乐场。
Swift 3.0 版本 - 按预期工作。
请注意,我必须创建 epytClass 才能让它在 Swift 3 操场上工作。
class emptyClass{
@objc public func testFunction(){
print ("It should work")
}
}
//MARK: observer //shouldnt bother us now.
let emptyClassRef = emptyClass()
NotificationCenter.default.addObserver(emptyClassRef, selector: #selector(emptyClass.testowFunction), name: NSNotification.Name(rawValue: "test0"), object: nil)
//MARK: post
let messageObject0 = AlertNotificationObject(message: "Test1")
let messageDict0 = [1: messageObject0]
let test0 = NSNotification.init(name: NSNotification.Name(rawValue: "test0"), object: nil, userInfo: messageDict0)
NotificationCenter.default.post(test0 as Notification)
应用内 Swift 2.3 版本 - 除了明显的语法更改外,我无法让它正常工作 - 我卡在了通知发布中。
let messageObject0 = AlertNotificationObject(message: "test")
let messageDict = [1: messageObject0]
let showAlertWithBlur = NSNotification.init(name: "ShowAlertBlur", object: nil, userInfo: messageDict)
NSNotificationCenter.defaultCenter().postNotification(showAlertWithBlur)
我遇到了一个错误...我想了解这里出了什么问题,最重要的是要知道 为什么它在 Swift 3 中有效。欢迎提出任何建议。
【问题讨论】:
标签: dictionary swift3 structure swift-playground swift2.3