OGeek|极客世界-中国程序员成长平台

标题: ios - 将 ManagedObjectID 存储在 NSDictionary 中 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:50
标题: ios - 将 ManagedObjectID 存储在 NSDictionary 中

我正在处理本地通知,为了识别每个通知,它需要从核心数据模型中获取一个 NSDictionary,其中 ManagedObjectID 作为该字典的键。

问题是当我这样做时,我得到以下错误:

Property list invalid for format: 200 (property list dictionaries may only have keys which are CFStrings, not 'CFType')

这与字典只能存储某些数据类型而不是 NSManagedObjectID 的事实有关。

所以我的问题是,有没有办法将此 NSManagedObjectId 转换为 NSStringNSNumber 例如。我真的需要那个特定的 id 以便以后识别它以删除那个特定的通知。

到目前为止的代码:

    -(void) scheduleNotificationAtDateNSDate*)date WithBodyNSString*) body WithBillEventIdNSManagedObjectID*) billEventId{

// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.alertBody = body;
localNotification.alertAction = @"Ga naar de Rekening Delen app";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

//set the dictionary here,
NSDictionary *billEventIdentifier = [NSDictionary dictionaryWithObject"billevent_notification" forKey: billEventId];
localNotification.userInfo = billEventIdentifier;
NSLog(@"set local notification");
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}



Best Answer-推荐答案


您遇到的问题是,要使对象正确存储在字典中,然后序列化到磁盘,它们必须符合 NSCoding 而 NSManagedObjectID 不符合。

幸运的是,还有另一种方法。 NSManagedObjectID 上有一个名为 URIRepresentation 的方法,它将成为 Core Data 对象的标识符,您可以使用它来代替存储 NSManagedObjectID。然后,您可以将该 NSURL URIRepresentation 转换回 NSManagedObjectID,如下例所示:

NSManagedObjectID *objectID = [context.persistentStoreCoordinator managedObjectIDForURIRepresentation:myObjectURI];

关于ios - 将 ManagedObjectID 存储在 NSDictionary 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658030/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4