Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification in Swift returning UserInfo in dictionary

Tags:

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

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
}
like image 936
Paul S. Avatar asked Sep 19 '14 12:09

Paul S.


2 Answers

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]

like image 99
Austen Chongpison Avatar answered Sep 22 '22 12:09

Austen Chongpison


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)
like image 2
Ramesh Avatar answered Sep 23 '22 12:09

Ramesh