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])
}
Here is the notification observer:
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
}
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
@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
}
Your userInfo
dictionary is wrong.
You have:
[NSObject():selectedDateDictionary]
Try setting userInfo
to userInfo: selectedDateDictionary
like this:
NSNotificationCenter.defaultCenter().postNotificationName("dateOfBirth", object: nil, userInfo: selectedDateDictionary)
The error is telling you that you are expecting a String
because of returnedDateOfBirth:String
but you return [NSObject():selectedDateDictionary]
Try it Only dictionary can pass to userInfo
var pageDict: Dictionary<String,String>! = [
"dateOfBirth": "11/5/1987date",
]
NSNotificationCenter.defaultCenter().postNotificationName("dateOfBirth", object: nil, userInfo: pageDict)
or
NSNotificationCenter.defaultCenter().postNotificationName("dateOfBirth", object: pageDist, userInfo: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With