我正在尝试在我的应用中实施UNUserNotification。一般情况下它可以工作,但我需要用userInfo传递我的对象(后来才接收它):
static func showNotification(_ serverNotification: ServerNotification){
print("showNotification called")
let content = UNMutableNotificationContent()
content.title = serverNotification.title
content.body = serverNotification.notificationDescription
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "pl.tropicalapps.TryOnNotification"
content.userInfo = ["serverNotification" : serverNotification]
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "Request", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error:Error?) in
if let err = error {
print(err)
}
else{
print("notification sent!")
}
}
}
如果我不添加userInfo或传递[“foo”:“bar”],它就可以了。但是当我添加我的对象(serverNotification)时,它会在发送请求后崩溃。我无法捕捉到这个错误,但它出现在` - [NSXPCEncoder _checkObject:] :(其余的是汇编程序)。
这是ServerNotification类:
class ServerNotification : NSCoder {
var id:Int
var type:String
var brandId:Int
var title:String
var notificationDescription:String = ""
var discountId:String
var discountPercent:Double
var discountDescription:String
var discountCode:String
var discountType:String
var discountExpireDate:Date
var date:Date
override init(){
id = 0
type = ""
brandId = 0
title = ""
notificationDescription = ""
discountId = ""
discountPercent = 0.0
discountDescription = ""
discountCode = ""
discountType = ""
discountExpireDate = Date()
date = Date()
}
required init(coder aDecoder: NSCoder) {
id = aDecoder.decodeInteger(forKey: "id")
type = aDecoder.decodeObject(forKey: "type") as! String
brandId = aDecoder.decodeInteger(forKey: "brandId")
title = aDecoder.decodeObject(forKey: "title") as! String
notificationDescription = aDecoder.decodeObject(forKey: "notificationDescription") as! String
discountId = aDecoder.decodeObject(forKey: "discountId") as! String
discountPercent = aDecoder.decodeDouble(forKey: "discountPercent")
discountDescription = aDecoder.decodeObject(forKey: "discountDescription") as! String
discountCode = aDecoder.decodeObject(forKey: "discountCode") as! String
discountType = aDecoder.decodeObject(forKey: "discountType") as! String
discountExpireDate = aDecoder.decodeObject(forKey: "discountExpireDate") as! Date
date = aDecoder.decodeObject(forKey: "date") as! Date
super.init()
}
func encodeWithCoder(_ aCoder: NSCoder){
aCoder.encode(id, forKey: "id")
aCoder.encode(type, forKey: "type")
aCoder.encode(brandId, forKey: "brandId")
aCoder.encode(title, forKey: "title")
aCoder.encode(notificationDescription, forKey: "notificationDescription")
aCoder.encode(discountId, forKey: "discountId")
aCoder.encode(discountPercent, forKey: "discountPercent")
aCoder.encode(discountDescription, forKey: "discountDescription")
aCoder.encode(discountCode, forKey: "discountCode")
aCoder.encode(discountType, forKey: "discountType")
aCoder.encode(discountExpireDate, forKey: "discountExpireDate")
aCoder.encode(date, forKey: "date")
}
}
问题出在哪里?我怎样才能解决这个问题? 我在使用iOS 10.2的设备上运行应用程序
答案 0 :(得分:2)
就我使用模拟器测试您的代码而言,更多关注 -[NSXPCEncoder _checkObject:]:
<强>
This coder only encodes objects that adopt NSSecureCoding (object is of class 'TestProjectName.ServerNotification').
强>
您可能需要将ServerNotification
的标题设为:
class ServerNotification : NSObject, NSSecureCoding {
Xcode会显示一些右侧“Fix-it”的建议。
当你想要创建自己的归档器/ unarchiver时,你通常是NSCoder
的子类,除此之外,这样的子类化是没用的。