当前位置:  开发笔记 > IOS > 正文

Swift 3.0:对推送通知中成员'下标'问题的模糊引用

如何解决《Swift3.0:对推送通知中成员'下标'问题的模糊引用》经验,为你挑选了1个好方法。

这是以下代码,但我在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 ?? "-")
    }

但我得到以下错误" Swift 3.0:对成员'下标'问题的模糊引用 "在" 让aps = notification ["aps"]为"NSDictionary "



1> Anbu.Karthik..:

键入转换

在此输入图像描述

将userInfo从NSDictionary更改为[String:Any].并尝试一次

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

或者写得像

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

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有