I got this error in swift3:
Cannot convert value of type [AnyHashable : Any] to type NSDictionary in coercion.
My Code is:
func downloadProgress(notification:NSNotification){
if let userInfo = notification.userInfo as NSDictionary
{
print(userInfo) // [AnyHashable("progressPercentage"): 0.82530790852915281]
if let progressValue = userInfo["progressPercentage"] as? Float {
if progressValue > 0.01{
}
}
}
}
The actual userInfo value is ["progressPercentage": 0.82530790852915281]
but its printed as [AnyHashable("progressPercentage"): 0.82530790852915281]
and not satisfying the if
condition.
Edit:
No luck for notification.userInfo as? [AnyHashable: Any]
, But now works for notification.userInfo as? [String: AnyObject]
and notification.userInfo as? [String: Any]
type converson
use
func downloadProgress(notification:NSNotification){
if let userInfo = notification.userInfo as [String: Any] // or use if you know the type [AnyHashable : Any]
{
print(userInfo)
if let progressValue = userInfo["progressPercentage"] as? Float {
if progressValue > 0.01{
}
}
}
}
for more information see this API Reference Documents
Plase use the new class Notification
instead of NSNotification
.
I haven't my Mac with me, but this should fix the problem.
This change is applied automatically if you use the integrated Xcode Swift conversion tool.
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