【问题标题】:Swift 3.0 : Ambiguous reference to member 'Subscript' issue in push notificationSwift 3.0:推送通知中对成员“下标”问题的模糊引用
【发布时间】:2017-01-20 10:24:38
【问题描述】:

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

但我在“let aps = notification["aps"] as?NSDictionary 处收到以下错误“Swift 3.0 : Ambiguous reference to member 'Subscript' issue” >"

【问题讨论】:

    标签: ios swift3 xcode8.1


    【解决方案1】:

    类型转换

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

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

    或者像这样写

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

    【讨论】:

      【解决方案2】:

      字符串不能转换为 NSObject,只需使用 Swift 3 将所有 [NSObject:AnyObject] 重命名为 [String:Any]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-12
        • 2016-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多