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
isnil
,userInfo?["ID"]
returnsnil
due to optional chaining and the conditional cast returns a variable of typeString?
that has a value ofnil
. The optional binding then fails and the block is not entered.If
"ID"
is not a valid key in the dictionary,userInfo?["ID"]
returnsnil
and it proceeds like the previous case.If the value is another type (like
Int
), then the conditional castas?
will returnnil
and it proceeds like the above cases.Finally, if
userInfo
is notnil
, and"ID"
is a valid key in the dictionary, and the type of the value is aString
, then the conditional cast returns an optional stringString?
containing the string. The optional bindingif let
then unwraps theString
and assigns it tos
which will have typeString
.
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
How to Create a Cocoapod with .Swift
Difference Between Object(Forkey:) and Value(Forkey:) in Userdefaults
Swift - Measurement Convert(To:) Miles to Feet Gives Wrong Result
Swiftui CPU High Usage on Real-Time Foreach View Updating (Macos)
Reading from Txt File in Swift 3
Set an Horizontal Scroll to My Barchart in Swift
Accessibility Custom Actions Aren't Announced in Swift
Member Operator '==' Must Have at Least One Argument of Type
Pass Type to Generic Function and Compare
Geocodeaddressstring Not Working for Swift
Swift 4 - Hmcharacteristictypeserialnumber Deprecated
Cocoa: Simulating Command+Tab in Cgevent
Reachability Change Notification Should Be Called Only Once
How to Present a UIcollectionview in Swiftui with UIviewcontrollerrepresentable