Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3.0 : Ambiguous reference to member 'Subscript' issue in push notification

This is the following code but i am getting this following error in swift3

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    notificationReceived(notification: userInfo as [NSObject : AnyObject])
}

func notificationReceived(notification: [NSObject:AnyObject]) {
        let viewController = window?.rootViewController
        let view = viewController as? ViewController
        view?.addNotification(
            title: getAlert(notification: notification).0,
            body: getAlert(notification: notification).1)
    }

    private func getAlert(notification: [NSObject:AnyObject]) -> (String, String) {
        let aps = notification["aps"] as? NSDictionary
        let alert = aps?["alert"] as? [String:AnyObject]
        let title = alert?["title"] as? String
        let body = alert?["body"] as? String
        return (title ?? "-", body ?? "-")
    }

But i am getting the following error "Swift 3.0 : Ambiguous reference to member 'Subscript' issue" at "let aps = notification["aps"] as? NSDictionary"

like image 865
Jessica thompson Avatar asked Jan 20 '17 10:01

Jessica thompson


2 Answers

type converson

enter image description here

Change userInfo from NSDictionary to [String : Any]. and try once

let aps = notification["aps"] as? [String : Any]

or write as like

let aps = notification["aps" as NSString] as? [String:Any]
like image 82
Anbu.Karthik Avatar answered Sep 21 '22 17:09

Anbu.Karthik


String is not convertable to NSObject, just rename all your [NSObject:AnyObject] to [String:Any] with Swift 3

like image 33
Tj3n Avatar answered Sep 19 '22 17:09

Tj3n