问题描述
我正在尝试将实体放入核心数据存储中,每个实体具有非标准的数据类型-一个是NSDocument,另一个是NSURL.我知道在.xcdatamodeld中需要将它们声明为Transformable.我认为我需要添加一些实现NSValueTransformer的东西,以将它们转换为NSData(...还是默认的转换器会自动执行此操作?)
I'm trying to put entities in a Core Data store that each have non-standard data types - one is an NSDocument and the other is an NSURL. I get that within the .xcdatamodeld I need to declare them as Transformable. And I think I need to add something that implements NSValueTransformer to turn them into NSData (... or is there a default transformer that will do that automatically?)
我认为这个问题应该很容易回答:我应该在哪里实现?我还没有找到在Swift中执行此操作的明确示例.有一个 Objective-C示例,但.h和.m文件让我不清楚它在Swift中的去向.
I think the question should be simple to answer: where do I implement it? I haven't found an explicit example of doing this in Swift. There's an Objective-C example but the separation of .h and .m files makes it unclear to me where this would go in Swift.
假设我有一个名为Notebook的实体类型,其属性名为folderURL-在数据库中为Transformable类型,在实际变量中为NSURL类型. (我将其用作示例,因为它是一个简单的示例-请不要告诉我仅将NSURL转换为String,因为这不适用于其他Transformable.)
Suppose I have an entity type called Notebook with an attribute called folderURL - of type Transformable in the database, and of type NSURL in the actual variable. (I'm using that as the example because it's the simpler one - please don't tell me to just convert the NSURL to a String, because that won't work for the other Transformable.)
问题是:假设需要一个NSURL,我将NSValueTransformer放在folderURL的什么位置?笔记本应该实施吗?应该使用Notebook + CoreDataProperties吗?还是我需要一个单独的称为FolderURL的子类来实现它,如果是的话,如何在Notebook(和/或Notebook + CoreDataProperties)中引用该子类?
Question is: where do I put the NSValueTransformer for folderURL, assuming that I need one? Should Notebook implement it? Should Notebook+CoreDataProperties? Or do I need a separate subclass called FolderURL that implements it, and if so, how do I refer to that subclass back in Notebook (and/or Notebook+CoreDataProperties)?
(以前有一个问题,它的标题类似类似的标题,但
(There was an earlier question with a similar title but they seem to have got a bit further than this already - they're not asking or demonstrating about the "where".)
推荐答案
位置"位于项目中的任何位置.确保您的NSValueTransformer
子类在运行时存在的任何位置.实现NSValueTransformer
子类,然后在Core Data模型中输入类名称作为值转换器.
The "where" is just anywhere in your project. Anywhere that ensures that your NSValueTransformer
subclass will exist at run time. Implement the NSValueTransformer
subclass, and enter the class name in the Core Data model as the value transformer.
但是是默认变压器.任何采用NSCoding
协议的类都可以由Core Data自动转换.在这种情况下,您将属性标记为可变形,但不包括类名.其中包括NSURL
,因此您无需对其进行转换或将其转换为字符串.
However there is a default transformer. Any class that adopts the NSCoding
protocol can be automatically transformed by Core Data. In that case you mark the attribute as transformable but don't include a class name. This includes NSURL
, so you don't need to transform that or convert it to a string.
那么对于您的NSDocument
子类,您可以选择在该类中实现NSCoding
或为文档类型实现一个NSValueTransformer
子类.
For your NSDocument
subclass then, you have the option of either implementing NSCoding
in the class or of implementing an NSValueTransformer
subclass for the document type.
这篇关于在Swift中为核心数据实现NSValueTransformer的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!