2017-01-20 1 views
3

Ceci est le code suivant, mais je reçois cette erreur suivante dans swift3Swift 3.0: Ambigu référence à un membre numéro « Subscript » dans la notification push

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 ?? "-") 
    } 

Mais je reçois l'erreur suivante "Swift 3.0: ambigu référence à un membre numéro 'Subscript' "à" laisser aps = notification [ "aps"] comme? NSDictionary "

Répondre

6

Type converson

enter image description here

Modifiez userInfo de NSDictionary à [String: Any]. et essayer une fois

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

ou écrire comme comme

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

chaîne n'est pas à ConverTable NSObject, renommer simplement tous vos [NSObject:AnyObject]-[String:Any] avec Swift 3