核心数据和瞬态属性

发布于 2024-10-14 14:22:24 字数 1455 浏览 7 评论 0原文

我在核心数据中设置瞬态属性时遇到问题。我试图解决的问题是:我有一个使用核心数据成功存储的对象集合。该集合本质上是一个只读集合,但在运行时,我想将另一个对象(在本例中为 UIImage)与集合中的每个项目相关联。我不需要存储 UIImages;它纯粹是一个运行时关联。所以我真正需要的是集合中每个对象中的“占位符”,稍后我可以在其中放置 UIImage。

瞬态属性似乎是正确的事情,为此,核心数据参考文档告诉我,我应该在我已经完成的数据模型中设置一个“可选的未定义类型的瞬态属性”: XCode snippet

此外,它告诉我,我不需要在实现对象中定义相应的属性,但我将拥有编写自定义访问器 &设置器方法。我根据文档中的示例对这些方法进行了建模,如下所示:

- (void)setDisplayImage:(UIImage *)anImage {

    [self willChangeValueForKey:@"displayImage"];
    [self setPrimitiveValue:anImage forKey:@"displayImage"];
    [self didChangeValueForKey:@"displayImage"];
}

并且:

- (UIImage *)displayImage {

    [self willAccessValueForKey:@"displayImage"];
    UIImage *anImage = [self primitiveDisplayImage];
    [self didAccessValueForKey:@"displayImage"];

    if (anImage == nil) {       
        NSData *displayImageData = [self displayImageData];
        if (displayImageData != nil) {
            anImage = [NSKeyedUnarchiver unarchiveObjectWithData:displayImageData];
            [self setPrimitiveDisplayImage:anImage];
        }
    }
    return anImage;
}

还有一个延迟的“写入”方法(willSave),我为了严格的完整性而编写了它,尽管如上所述,我永远不会写回这些对象存储。

我遇到的问题是 - (UIImage *)displayImage { 的 PrimitiveDisplayImage 和 setPrimitiveDisplayImage 方法。基本上,编译器会对这两种方法发出警告,如“对象 X 可能不会响应‘primitiveDisplayImage’..”等。 然而,我对文档的理解是,这些原始访问器/设置器是自动创建的 - 情况似乎并非如此。 有谁可以帮我解决这个问题吗? 非常感谢

I'm having trouble with setting up a transient property in Core Data. The problem I'm trying to solve is: I have a collection of objects successfully stored using Core Data. This collection is essentially a read-only collection but at run-time, I want to associate another object (a UIImage in this case) with each item in the collection. I do not need to store the UIImages; it is purely a run-time association. So what I really need is a 'placeholder' in each object in the collection where I can drop the UIImage later.

A transient attribute seems like the right thing and, for this, Core Data reference docs tell me that I should set up an "optional, transient attribute of type undefined" in the data model which I have done:
XCode snippet

Further, it tells me that I do not need to define a corresponding property in the implementation object but that I will have to write custom accessor & setter methods. I've modeled these methods based on the examples in the docs as follows:

- (void)setDisplayImage:(UIImage *)anImage {

    [self willChangeValueForKey:@"displayImage"];
    [self setPrimitiveValue:anImage forKey:@"displayImage"];
    [self didChangeValueForKey:@"displayImage"];
}

and:

- (UIImage *)displayImage {

    [self willAccessValueForKey:@"displayImage"];
    UIImage *anImage = [self primitiveDisplayImage];
    [self didAccessValueForKey:@"displayImage"];

    if (anImage == nil) {       
        NSData *displayImageData = [self displayImageData];
        if (displayImageData != nil) {
            anImage = [NSKeyedUnarchiver unarchiveObjectWithData:displayImageData];
            [self setPrimitiveDisplayImage:anImage];
        }
    }
    return anImage;
}

There is also a deferred "write" method (willSave) that I have written for strict completeness although, as above, I will never be writing these objects back to store.

The problem I am getting is in the primitiveDisplayImage and setPrimitiveDisplayImage methods of - (UIImage *)displayImage { . Basically, the compiler throws warnings on these two methods as "object X may not respond to 'primitiveDisplayImage'.." etc.
However, my understanding from the docs is that these primitive accessors/setters ARE created automatically - this does not seem to be the case.
Anyone out there who can help me solve this issue?
Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知在何时 2024-10-21 14:22:24

setPrimitive... 方法的问题在于,尽管 NSManagedObject 会在运行时响应它们,但编译器无法知道它们的存在。您可以通过使用类别在实体类的头文件中声明方法来抑制警告消息,这样编译器就不会因为您没有在实现中定义它而对您大喊大叫。

另一种选择是仅对您需要访问的每个属性调用 [self setPrimitiveValue:value forKey:@"key"]; 。我过去曾使用过这种方法,尽管我刚刚在文档中注意到在 10.5 及更高版本中不鼓励这样做。

The problem with setPrimitive... methods is that although NSManagedObject will respond to them at runtime, the compiler has no way of knowing they exist. You can suppress the warning messages by declaring the method in your entity class's header file, using a category so the compiler doesn't yell at you for not defining it in your implementation.

Another option is just using calling [self setPrimitiveValue:value forKey:@"key"]; for each of the attributes you need to access. I've used this method in the past, although I just noticed in the docs that this is discouraged on 10.5 and later.

许你一世情深 2024-10-21 14:22:24

在这里找到答案:托管对象访问器方法< /a>

基本上:在类别头文件中声明@propery,在类别实现文件中定义@dynamic

Found the answer here: Managed Object Accessor Methods

Basically: Declare @propery in category header file, define @dynamic in category implementation file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文