我不清楚为什么我不能使用以下方法将其保存到userprefs:
/** save the queue to prefs so it can be processed at a later time */
func saveBackgroundNotificationsToPrefs(_ backgroundNotification:[Date:[App:NotificationSpecification]]) {
UserDefaults.standard.set(backgroundNotification, forKey: PreferencesModel.constants.backgroundNotificationsKey)
}
我要保存的 NotificationSpecification 对象是:
internal var apps: [App] // this one saves in it's "saveAppsToPrefs" method
internal var backgroundNotifications: [Date:[App:NotificationSpecification]] // this one does not save.
我的应用程序结构是:
struct App: Hashable, Codable, Equatable {
let name: String
let notify: String
let id: String
let isSubscribed: Bool
}
,通知规范为:
struct NotificationSpecification: Hashable, Codable {
let id: String // is also the app id
let p8: String
let key_id: String?
let team_id: String?
let topic: String
let custom_endpoint: String?
let custom_method: String?
let custom_headers: [String:String]?
init (id:String, p8:String, key_id:String, team_id:String, topic:String) {
self.id = id
self.p8 = p8
self.key_id = key_id
self.team_id = team_id
self.topic = topic
self.custom_endpoint = nil
self.custom_method = nil
self.custom_headers = nil
}
init (id:String, p8:String, topic:String, custom_endpoint:String, custom_method:String, custom_headers:[String:String]?) {
self.id = id
self.p8 = p8
self.topic = topic
self.custom_endpoint = custom_endpoint
self.custom_method = custom_method
self.custom_headers = custom_headers
self.key_id = nil
self.team_id = nil
}
}
我收到的错误是:
2019-04-27 12:58:54.812658-0400 Behavior-based Notifications[15130:4568207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {
"" = {
"Behavior_based_Notifications.App(name: \"APP_NAME\", notify: \"{\\\"token\\\":\\\"APP_TOKEN\\\",\\\"body\\\":{\\\"to\\\":\\\"APP_TOKEN\\\",\\\"badge\\\":0,\\\"_category\\\":\\\"APP_CATEGORY\\\"}}\", id: \"APP_ID\", isSubscribed: true)" = "Behavior_based_Notifications.NotificationSpecification(id: \"SAME_APP_ID\", p8: \"custom\", key_id: nil, team_id: nil, topic: \"TOPIC\", custom_endpoint: Optional(\"HOST_URL"), custom_method: Optional(\"POST\"), custom_headers: Optional([\"host\": \"HOSTNAME\", \"content-type\": \"application/json\", \"accept-encoding\": \"gzip, deflate\", \"accept\": \"application/json\"]))";
};
} for key backgroundNotification'
是因为custom_headers吗?我该怎么办-我必须手动将其转换为json吗?
遵循Larme的建议:
/** save the queue to prefs so it can be processed at a later time */
func saveBackgroundNotificationsToPrefs(_ backgroundNotification:[Date:[App:NotificationSpecification]]) {
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: backgroundNotification), forKey: PreferencesModel.constants.backgroundNotificationsKey)
}
/** restore the queue from prefs so it can be processed at a later time */
func getBackgroundNotificationFromPrefs() -> [Date:[App:NotificationSpecification]] {
let defaultBackgroundNotification:[Date:[App:NotificationSpecification]] = [:]
guard let backgroundNotificationData = UserDefaults.standard.value(forKey:
PreferencesModel.constants.backgroundNotificationsKey) as? Data else {
saveBackgroundNotificationsToPrefs(defaultBackgroundNotification)
return defaultBackgroundNotification
}
guard let backgroundNotification = NSKeyedUnarchiver.unarchiveObject(with: backgroundNotificationData) as? [Date :
[App:NotificationSpecification]] else {
return defaultBackgroundNotification
}
return backgroundNotification
}
我得到:
'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead
和
'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead
如果我将其更改为NSKeyedArchiver.archivedDataWithRootObject( backgroundNotification)
它告诉我该功能已重命名为生成警告的形式。