gpt4 book ai didi

ios - 如何持久存储 NSManagedObjectID?

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:13 24 4
gpt4 key购买 nike

为避免成为 XY 问题,这里有一些背景:

我的应用程序允许用户创建和保存很多设置,有点像 Xcode 的字体和颜色选择器:

enter image description here

这是因为用户可以设置的东西很多。只需点击保存的设置而不是重新设置所有这些设置会更容易。

我使用 Core Data 来存储用户保存的设置。用户创建的每个设置都是 NSManagedObject 子类的一个实例。现在我需要永久存储所选设置,以便用户在应用重新打开时选择与之前相同的设置。

我的第一个想法是将 NSManagedObject 子类实例存储在 NSUserDefaults 中。但是根据文档,除非将其转换为 NSData,否则我无法存储它:

A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

然后我尝试存储 NSManagedObject 子类实例的 objectID。我看到有一个返回 NSURLURIRepresentation() 方法。所以我认为这会起作用:

NSData(contentsOfURL: selectedOption!.objectID.URIRepresentation())

但是初始化程序不知何故失败了。

现在我意识到这是一个愚蠢的想法,因为即使我可以将它转换为 NSData,我也无法将 NSData 转换回 NSManagedObjectID!

根据 this question ,OP 似乎能够存储对象 ID:

I store the selected theme objectID in NSUserDefaults so that when the app restarts, the selected theme will still be intact.

我该怎么做?

最佳答案

NSUserDefaults 有“便捷方法”

public func setURL(url: NSURL?, forKey defaultName: String)
public func URLForKey(defaultName: String) -> NSURL?

它允许存储和检索一个 NSURL 就像获得的那样通过 URIRepresentation()。与 NSData 之间的转换透明处理。来自documentation :

When an NSURL is stored using -[NSUserDefaults setURL:forKey:], some adjustments are made:

  1. Any non-file URL is written by calling +[NSKeyedArchiver archivedDataWithRootObject:] using the NSURL instance as the root object.
  2. ...

When an NSURL is read using -[NSUserDefaults URLForKey:], the following logic is used:

  1. If the value for the key is an NSData, the NSData is used as the argument to +[NSKeyedUnarchiver unarchiveObjectWithData:]. If the NSData can be unarchived as an NSURL, the NSURL is returned otherwise nil is returned.
  2. ...

因此保存托管对象 ID 的操作非常简单

NSUserDefaults.standardUserDefaults().setURL(object.objectID.URIRepresentation(),
forKey: "selected")

并检索对象 ID 和对象,例如:

if let url = NSUserDefaults.standardUserDefaults().URLForKey("selected"),
let oid = context.persistentStoreCoordinator!.managedObjectIDForURIRepresentation(url),
let object = try? context.existingObjectWithID(oid) {

print(object)
// ...
}

有关保存所选设置的替代方法,请参阅 the above comment .

关于ios - 如何持久存储 NSManagedObjectID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38953560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com