Notification in Swift Returning Userinfo in Dictionary

Notification in Swift returning UserInfo in dictionary

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]

(iOS 10, Swift 3) Reading `userInfo` dictionary from a CloudKit notification: How do I cast `[AnyHashable : Any]` to `[String : NSObject]`?

You just need to cast it first to NSDictionary and then you can cast it to [String: NSObject].

Try like this:

CKNotification(fromRemoteNotificationDictionary: userInfo as NSDictionary as! [String: NSObject])

Why some Notifications.userInfo is nil?

There is no "why" and no, you can't change this. These notifications come from the runtime. Some kinds of notification come with extra info in the userInfo, others don't. The way it works is the way it works. The end.

This is all perfectly well documented. The didActivate documentation says:

The notification object is the shared NSWorkspace instance. The userInfo dictionary contains the applicationUserInfoKey key with a corresponding instance of NSRunningApplication that represents the affected app.

So there's your user info. But the activeSpace documentation says:

The notification doesn’t contain a userInfo dictionary.

There you go.

Get string from userInfo Dictionary

You want to use a condition cast using as?:

(Note: This works for Xcode 6.1. For Xcode 6.0, see below)

if let s = userInfo?["ID"] as? String {
// When we get here, we know "ID" is a valid key
// and that the value is a String.
}

This construct safely extracts a string from userInfo:

  • If userInfo is nil, userInfo?["ID"] returns nil due to optional chaining and the conditional cast returns a variable of type String? that has a value of nil. The optional binding then fails and the block is not entered.

  • If "ID" is not a valid key in the dictionary, userInfo?["ID"] returns nil and it proceeds like the previous case.

  • If the value is another type (like Int), then the conditional cast as? will return nil and it proceeds like the above cases.

  • Finally, if userInfo is not nil, and "ID" is a valid key in the dictionary, and the type of the value is a String, then the conditional cast returns an optional string String? containing the string. The optional binding if let then unwraps the String and assigns it to s which will have type String.


For Xcode 6.0, there is one additional thing you must do. You need to conditionally cast to NSString instead of to String because NSString is an object type and String is not. They apparently improved that handling in Xcode 6.1, but for Xcode 6.0 do the following:

if let s:String = userInfo?["ID"] as? NSString {
// When we get here, we know "ID" is a valid key
// and that the value is a String.
}

Finally, addressing your last point:

  for notification in scheduledNotifications
{
if let id:String = notification.userInfo?["ID"] as? NSString
{
println( "Id found: " + id )
}
else
{
println( "ID not found" )
}
}

Swift read userInfo of remote notification

The root level item of the userInfo dictionary is "aps", not "alert".

Try the following:

if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
//Do stuff
}
} else if let alert = aps["alert"] as? NSString {
//Do stuff
}
}

See Push Notification Documentation

Swift Dictionary in UILocalNotification userinfo

You are creating a new UILocalNotification so its userInfo is nil; there is nothing there to cast. You have to create the userInfo as some definite Swift dictionary type if you want to treat it as a Swift dictionary (as you are attempting to do). Also, you need an actual value for medicine, not just an uninitialized type. For example:

var userInfo = [String:String]()
let medicine = "SomeMedicine"
userInfo["medicine"] = medicine
notification.userInfo = userInfo


Related Topics



Leave a reply



Submit