簡體   English   中英

Swift中的通知在字典中返回UserInfo

[英]Notification in Swift returning UserInfo in dictionary

我有一個返回字典的通知,就像在objective-c中一樣,但我沒有得到我期望的結果。

這是發布通知的方法。 它實際上返回了日期選擇器的結果(日期)。

@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"]上收到以下錯誤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

完整的解決方案

@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)

錯誤告訴你因為returnedDateOfBirth:String而你期待一個String ,但你返回[NSObject():selectedDateDictionary]

試一試只有字典可以傳遞給userInfo

        var pageDict: Dictionary<String,String>! = [
            "dateOfBirth": "11/5/1987date",
        ]
        NSNotificationCenter.defaultCenter().postNotificationName("dateOfBirth", object: nil, userInfo: pageDict)

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM