Swift中的通知在字典中返回UserInfo [英] Notification in Swift returning UserInfo in dictionary

查看:165
本文介绍了Swift中的通知在字典中返回UserInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回字典的通知,就像在objective-c中一样,但我没有得到我期望的结果。

I have a Notification that returns a dictionary, much like in objective-c, but I am not getting the results I expect.

这是方法发布通知。它实际上是返回日期选择器的结果(日期)。

Here is the method that is posting the Notification. It's actually returning the results (date) of date picker.

@IBAction func dateOfBirthAction(sender: AnyObject) {

    println("Date: \(dateOfBirthPicker.date)")

    var selectedDateDictionary = [dateOfBirthPicker.date, "dateOfBirth"]

    NSNotificationCenter.defaultCenter().postNotificationName("dateOfBirth", object: nil, userInfo: [NSObject():selectedDateDictionary])

}

以下是通知观察员:

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "dateOfBirth:", name: "dateOfBirth", object: nil)

}

func dateOfBirth(notification: NSNotification) {

    println("userInfo: \(notification.userInfo)")

    let returnedDateOfBirth = notification.userInfo!["dateOfBirth"] as NSString
    println("returnedDateOfBirth: \(returnedDateOfBirth)")

    playerInformationDateOfBirthLabel.text  = returnedDateOfBirth
}

我在线路上收到以下错误 let returnedDateOfBirth:String = notification.userInfo [dateOfBirth]

I'm getting the following error on the line line let returnedDateOfBirth:String = notification.userInfo["dateOfBirth"]

userInfo: Optional([<NSObject: 0x7fb15a44a880>: <_TtCSs23_ContiguousArrayStorage00007FB15A4D4380 0x7fb15a4206a0>(
2014-09-18 12:07:07 +0000,
dateOfBirth
)
])
fatal error: unexpectedly found nil while unwrapping an Optional value



完整解决方案



FULL SOLUTION

@IBAction func dateOfBirthAction(sender: AnyObject) {

    var selectedDateDictionary = ["birthDate" : dateOfBirthPicker.date]

    NSNotificationCenter.defaultCenter().postNotificationName("foundABirthDate", object: nil, userInfo:selectedDateDictionary)
}

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "dateOfBirth:", name: "foundABirthDate", object: nil)

}

func dateOfBirth(notification: NSNotification) {

    let playerBirthDate = notification.userInfo!["birthDate"] as NSDate

    var dateFormatter: NSDateFormatter = NSDateFormatter()

    //Specifies a long style, typically with full text, such as "November 23, 1937".
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle

    let dateConvertedToString: String = dateFormatter.stringFromDate(playerBirthDate)

    playerInformationDateOfBirthLabel.text  = dateConvertedToString
}


推荐答案

您的 userInfo 字典错误。

你有:

[NSObject():selectedDateDictionary]

尝试设置 userInfo userInfo:selectedDateDictionary 像这样:

NSNotificationCenter.defaultCenter().postNotificationName("dateOfBirth", object: nil, userInfo: selectedDateDictionary)

错误告诉你你期待 String 因为 returnedDateOfBirth:String 但是你返回 [NSObject():selectedDateDictionary ]

The error is telling you that you are expecting a String because of returnedDateOfBirth:String but you return [NSObject():selectedDateDictionary]

这篇关于Swift中的通知在字典中返回UserInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆