2017-01-20 69 views
3

這是下面的代碼,但我得到這個下面的錯誤在swift3雨燕3.0:不明確的參考成員「下標」問題在推送通知

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

但我得到以下錯誤「雨燕3.0:曖昧參考構件 '下標' 問題 「在」 讓APS =通知[ 「APS」]作爲?的NSDictionary

回答

6

類型converson

enter image description here

將userInfo從NSDictionary更改爲[String:Any]。並嘗試一次

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

或寫像

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

字符串不是可轉換到NSObject,只是重新命名所有的[NSObject:AnyObject][String:Any]與斯威夫特3