UILocalNotification 的 userInfo 对象的一致性要求
conformity requirements for userInfo object of UILocalNotification
使用Swift-2.2,
我想将 'struct' 或 'class object' 传递给 UILocalNotification 的 userInfo。 (参见下面的代码图)。
你能告诉我需要如何更改此结构才能符合 UserInfo 的要求吗?
我读了一些关于
a) UserInfo 不能是结构体(但我也尝试使用 class - 它也不起作用)
b) "plist type" 一致性 --> 但我该怎么做呢?
c) "NSCoder" 和 "NSObject" 一致性 --> 但我该怎么做呢?
我收到的错误消息 运行 下面的代码是:
"unable to serialize userInfo"
感谢您对此的任何帮助。
struct MeetingData {
let title: String
let uuid: String
let startDate: NSDate
let endDate: NSDate
}
let notification = UILocalNotification()
notification.category = "some_category"
notification.alertLaunchImage = "Logo"
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
notification.alertBody = "Data-Collection Request!"
// notification.alertAction = "I want to participate"
notification.soundName = UILocalNotificationDefaultSoundName
let myData = MeetingData(title: "myTitle",
uuid: "myUUID",
startDate: NSDate(),
endDate: NSDate(timeIntervalSinceNow: 10))
// that's where everything crashes !!!!!!!!!!!!!!
notification.userInfo = ["myKey": myData] as [String: AnyObject]
正如 UILocalNotification.userInfo
的文档所说:
You may add arbitrary key-value pairs to this dictionary. However, the keys and values must be valid property-list types; if any are not, an exception is raised.
您需要自己将数据转换为这种类型。你可能想做这样的事情:
enum Keys {
static let title = "title"
static let uuid = "uuid"
static let startDate = "startDate"
static let endDate = "endDate"
}
extension MeetingData {
func dictionaryRepresentation() -> NSDictionary {
return [Keys.title: title,
Keys.uuid: uuid,
Keys.startDate: startDate,
Keys.endDate: endDate]
}
init?(dictionaryRepresentation dict: NSDictionary) {
if let title = dict[Keys.title] as? String,
let uuid = dict[Keys.uuid] as? String,
let startDate = dict[Keys.startDate] as? NSDate,
let endDate = dict[Keys.endDate] as? NSDate
{
self.init(title: title, uuid: uuid, startDate: startDate, endDate: endDate)
} else {
return nil
}
}
}
然后你可以使用myData.dictionaryRepresentation()
转换成字典,MeetingData(dictionaryRepresentation: ...)
从字典转换。
使用Swift-2.2,
我想将 'struct' 或 'class object' 传递给 UILocalNotification 的 userInfo。 (参见下面的代码图)。
你能告诉我需要如何更改此结构才能符合 UserInfo 的要求吗?
我读了一些关于
a) UserInfo 不能是结构体(但我也尝试使用 class - 它也不起作用)
b) "plist type" 一致性 --> 但我该怎么做呢?
c) "NSCoder" 和 "NSObject" 一致性 --> 但我该怎么做呢?
我收到的错误消息 运行 下面的代码是:
"unable to serialize userInfo"
感谢您对此的任何帮助。
struct MeetingData {
let title: String
let uuid: String
let startDate: NSDate
let endDate: NSDate
}
let notification = UILocalNotification()
notification.category = "some_category"
notification.alertLaunchImage = "Logo"
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
notification.alertBody = "Data-Collection Request!"
// notification.alertAction = "I want to participate"
notification.soundName = UILocalNotificationDefaultSoundName
let myData = MeetingData(title: "myTitle",
uuid: "myUUID",
startDate: NSDate(),
endDate: NSDate(timeIntervalSinceNow: 10))
// that's where everything crashes !!!!!!!!!!!!!!
notification.userInfo = ["myKey": myData] as [String: AnyObject]
正如 UILocalNotification.userInfo
的文档所说:
You may add arbitrary key-value pairs to this dictionary. However, the keys and values must be valid property-list types; if any are not, an exception is raised.
您需要自己将数据转换为这种类型。你可能想做这样的事情:
enum Keys {
static let title = "title"
static let uuid = "uuid"
static let startDate = "startDate"
static let endDate = "endDate"
}
extension MeetingData {
func dictionaryRepresentation() -> NSDictionary {
return [Keys.title: title,
Keys.uuid: uuid,
Keys.startDate: startDate,
Keys.endDate: endDate]
}
init?(dictionaryRepresentation dict: NSDictionary) {
if let title = dict[Keys.title] as? String,
let uuid = dict[Keys.uuid] as? String,
let startDate = dict[Keys.startDate] as? NSDate,
let endDate = dict[Keys.endDate] as? NSDate
{
self.init(title: title, uuid: uuid, startDate: startDate, endDate: endDate)
} else {
return nil
}
}
}
然后你可以使用myData.dictionaryRepresentation()
转换成字典,MeetingData(dictionaryRepresentation: ...)
从字典转换。