Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios userinfo中带有自定义对象的UNMutableNotificationContent_Ios_Swift_Notifications_Userinfo_Nssecurecoding - Fatal编程技术网

Ios userinfo中带有自定义对象的UNMutableNotificationContent

Ios userinfo中带有自定义对象的UNMutableNotificationContent,ios,swift,notifications,userinfo,nssecurecoding,Ios,Swift,Notifications,Userinfo,Nssecurecoding,我想在UNMutableNotificationContent的userinfo中使用cutom对象,但它不起作用。 在userinfo中放置自定义对象时,不会触发通知 使用此代码,将触发一个通知: let content = UNMutableNotificationContent() content.title = "title" content.body = "body" content.categoryIdentifier = "alarmNotificationCategory" co

我想在UNMutableNotificationContent的userinfo中使用cutom对象,但它不起作用。 在userinfo中放置自定义对象时,不会触发通知

使用此代码,将触发一个通知:

let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.categoryIdentifier = "alarmNotificationCategory"
content.sound = UNNotificationSound.default()
content.userInfo = ["myKey": "myValue"] as [String : Any]


let request = UNNotificationRequest(identifier: "alarmNotification", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
    UNUserNotificationCenter.current().delegate = self
    if error != nil {
        print(error!)
    }
}
在以下情况下,不会出现错误,但不会触发通知:

let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.categoryIdentifier = "alarmNotificationCategory"
content.sound = UNNotificationSound.default()
content.userInfo = ["myKey": TestClass(progress: 2)] as [String : Any]


let request = UNNotificationRequest(identifier: "alarmNotification", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
    UNUserNotificationCenter.current().delegate = self
    if error != nil {
        print(error!)
    }
}
TestClass是自定义类,定义如下:

class TestClass: NSObject, NSSecureCoding {
    public var progress: Float = 0

    required override public init() {
        super.init()
    }

    public init(progress: Float) {
        self.progress = progress
    }

    public required convenience init?(coder aDecoder: NSCoder) {
        self.init()
        progress = aDecoder.decodeObject(forKey: "progress") as! Float
    }

    public func encode(with aCoder: NSCoder) {
        aCoder.encode(progress, forKey: "progress")
    }

    public static var supportsSecureCoding: Bool {
        get {
            return true
        }
    }

}

有什么想法吗?

您的对象应该是属性列表

此字典中的键必须是属性列表类型,即 必须是可以序列化为属性列表格式的类型。 有关特性列表类型的信息,请参见特性列表 编程指南


您可以将对象转换为NSData(使用NSArchiver对其进行存档)

我也有同样的问题,例如,我希望在userInfo中传递UIImage,而not NOTITIONG根本不会触发。似乎只能传递基本类型(Int、String、float等)。