gpt4 book ai didi

core-data - 使用核心数据实现 transient 属性

转载 作者:行者123 更新时间:2023-12-02 23:17:07 25 4
gpt4 key购买 nike

我无法弄清楚这一点。我所读到的有关 transient 属性的内容告诉我,它们可以在对象模型中以未定义的类型进行识别。但编译器提示这一点,并显示类型未知的错误。

来自核心数据编程指南:

If the non-supported attribute is an object, then in the managed object model you specify its type as undefined, and that it is transient. When you implement the entity’s custom class, there is no need to add an instance variable for the attribute—you can use the managed object's private internal store. A point to note about the implementations described below is that they cache the transient value. This makes accessing the value more efficient—it is also necessary for change management. If you define custom instance variables, you should clean up these variables in didTurnIntoFault rather than dealloc or finalize.

这是头文件:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class SearchTerms;

@interface SearchResult : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * lattitude;
@property (nonatomic, retain) NSString * details;
@property (nonatomic, retain) NSString * endTime;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * imageLink;
@property (nonatomic, retain) NSString * startTime;
@property (nonatomic, retain) UNKNOWN_TYPE coordinate;
@property (nonatomic, retain) UNKNOWN_TYPE subtitle;
@property (nonatomic, retain) SearchTerms * searchUsed;

@end

我试图包含 MKAnnotation 的属性,其中包含 title、subtitle 和 axis 。这里,我需要从其他字段导出字幕,并从经度和纬度导出坐标。

我不确定如何调和指南所说的内容和看起来明显错误的内容,编译器是这么说的。

一旦我得到正确的标题,我也许能够得到正确的实现,并且我将使用 awakeFromFault 来设置值。我不确定是否需要使用 didTurnIntoFault 释放字幕,该字幕将是一个 NSString,但这似乎是指南所说的。

我还没有看到关于如何实现简单 transient 属性的真正好的示例。我很想直接将属性添加到托管对象实体中,而忘记在托管对象模型中提及它。但如果我这样做的话,我似乎会忽略一些事情。

最佳答案

您需要将属性的类型更改为id,或任何最合适的类型:

@interface SearchResult : NSManagedObject
{}
@property (nonatomic, retain) id coordinate;
@end

处理此问题的另一种方法是通过 KVC 和相关键:

@implementation SearchResult
+ (NSSet *) keyPathsForValuesAffectingCoordinate
{
return [NSSet setWithObjects:@"latitude", @"longitude", nil];
}

- (id) coordinate
{
// Derive the coordinate value
}
@end

关于core-data - 使用核心数据实现 transient 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7196031/

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