无法将类型'__NSCFString'的值强制转换为'NSArray'

时间:2019-07-14 14:19:31

标签: ios swift firebase-cloud-messaging

我有一个来自服务器端的通知有效负载(Firebase云消息传递),它由通常的 userInfo 和一个整数数组组成,我找不到在下面转换该数组的方法“ records_id” 键以 [Int]

这是我正在处理的 userInfo 的结构:

[
    AnyHashable("title"): title, 
    AnyHashable("sound"): true, 
    AnyHashable("aps"): {
        alert =     {
            body = "notif body";
            title = "notif title";
        };
        "content-available" = 1;
        "mutable-content" = 1;
        sound = true;
    }, 
    AnyHashable("google.c.a.e"): 1, 
    AnyHashable("action"): stacked, 
    AnyHashable("record_id"): [61,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97], 
    AnyHashable("gcm.message_id"): 1563172453549130, 
    AnyHashable("gcm.notification.fromAPI"): true, 
    AnyHashable("gcm.notification.action"): stacked, 
    AnyHashable("body"): notif body, 
    AnyHashable("gcm.notification.record_id"): [61,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97], 
    AnyHashable("fromAPI"): true
]

这是我在 AppDelegate 中的代码:

    // receive notification while app in foreground
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        registerNotification(userInfo)

    }

    // register notifications records
    func registerNotification(_ userInfo: [AnyHashable : Any]) {

        print(userInfo) // this print the above userinfo structure that I show earlier 

        let ids = userInfo["record_id"] as! [Int]

        for id in ids {
            print("new record: \(id)")
        }
    }

上面的代码在尝试转换为 [Int] 时抛出以下错误:

Could not cast value of type '__NSCFString' (0x1bebadf60) to 'NSArray' (0x1bebae960).

1 个答案:

答案 0 :(得分:1)

您可以尝试

modal.appendChild(closeButtonElement);
        modal.appendChild(titleElement);
        modal.appendChild(contentImage);
        modal.appendChild(contentHeight);
        modal.appendChild(contentElement);
        $modalContainer.appendChild(modal);

        $modalContainer.classList.add('is-visible');

        $modalContainer.addEventListener('click', (e) => {                          //listening for an event (click) anywhere on the modalContainer
            var target = e.target;
            console.log(e.target)
            if (target === $modalContainer) {
                hideModal();
            }
        });
    };

    window.addEventListener('keydown', (e) => {                                     //listening for an event (ESC) of the browser window
        var $modalContainer = document.querySelector('#modal-container');
        if (e.key === 'Escape' && $modalContainer.classList.contains('is-visible')) {
            hideModal();
        }
    });