为什么我不能在通知中传递带有枚举键的字典

Why can't I pass a dictionary with an enum key in a Notification

我尝试在 Notification ([TestEnum: String]) 中传递带有 enum 作为键的字典。不幸的是,在收到通知后将字典类型转换为 [TestEnum: String] 失败。

enum TestEnum {
    case test
}

class NotificationTest {

    var sender: String = "" {
        didSet {
            NotificationCenter.default.post(name: Notification.Name(rawValue: "Test"), object: nil, userInfo: [TestEnum.test: "test"])
        }
    }

    init() {
        NotificationCenter.default.addObserver(self, selector: #selector(notificationReceived(_:)), name: Notification.Name(rawValue: "Test"), object: nil)
    }

    @objc func notificationReceived(_ notification: Notification) {
        print("Notification Received")
        guard let userInfo = notification.userInfo as? [TestEnum: String] else { return } // Type casting fails here even though userInfo shows a TestEnum key in debugger
        print(userInfo[.test])
    }

}

let test = NotificationTest()
test.sender = "blah"

但是,如果我使用 TestEnumrawValue 作为键,当 notification.userInfo 转换为 [String: String] 时它工作正常。

刚刚查看了 AnyHashable 的源代码,当您将 Hashable(您的枚举)转换为 AnyHashable 时,它被包裹在 属性 base AnyHashable。因此,它不能直接转换回您的枚举。这里使用 reduce[AnyHashable:Any] 转换为 [TestEnum:String]:

@objc func notificationReceived(_ notification: Notification) {
    print("Notification Received")
    guard let userInfo = notification.userInfo?.reduce(into: [TestEnum:String](), { (result, entry) in
        if let key = entry.key.base as? TestEnum
        {
            result[key] = entry.value as? String
        }
    }) else {
        print(notification.userInfo)
        return
    } // Type casting fails here even though userInfo shows a TestEnum key in debugger
    print(userInfo[.test])
}

并且因为AnyHashable符合CustomStringConvertible,所以可以直接转换为String