gpt4 book ai didi

ios - 如何以编程方式添加获取的属性(到数据模型)?

转载 作者:行者123 更新时间:2023-11-29 01:16:26 24 4
gpt4 key购买 nike

为了找到解决办法,在这里贴出一个问题:Core Data Lightweight Migration not working when a Fetched Property is present

在对实体执行任何其他操作之前,我正在尝试查看是否可以通过编程方式(在 DataModel 上)将 Fetched Property 添加到实体。

但是我在这一点上的尝试失败了。这是我使用的代码:

    NSEntityDescription *myEntity = [NSEntityDescription entityForName:@"EntityName"
inManagedObjectContext:context];
NSFetchedPropertyDescription *myFetchProp = [[NSFetchedPropertyDescription alloc] init];
[myFetchProp setName:@"myFetchPropName"];
[myEntity setProperties:[[myEntity properties] arrayByAddingObject:myFetchProp]];

最后一行发生崩溃,我在调试器中可以看到:

(lldb) bt
* thread #1: tid = 0xc2b17, 0x0000000180c3bf48 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000180c3bf48 libobjc.A.dylib`objc_exception_throw
frame #1: 0x00000001830e0b70 CoreData`-[NSManagedObjectModel(_NSInternalMethods) _throwIfNotEditable] + 80
frame #2: 0x00000001830aa0f8 CoreData`-[NSEntityDescription setProperties:] + 52
* frame #3: 0x000000010007b2b4 parl100`-[XYZLib transferDataFromDefaultStoreToUserStore](self=0x000000015654dba0,

最佳答案

创建字段

+(NSEntityDescription*)entityDescription
{
NSEntityDescription *entityDescription = [NSEntityDescription new];
entityDescription.name = @"EntityName";

entityDescription.managedObjectClassName = NSStringFromClass(self);

//Describe date attribute creation.
NSAttributeDescription *creationDateDescription =
[NSAttributeDescription new];
creationDateDescription.name = @"creationDate";
creationDateDescription.attributeType = NSDateAttributeType;
creationDateDescription.attributeValueClassName = @"NSDate";
creationDateDescription.defaultValue = nil;

//Describe archivedObject.
NSAttributeDescription *archivedObjectDescription =
[NSAttributeDescription new];
archivedObjectDescription.name = @"archivedObject";
archivedObjectDescription.attributeType = NSBinaryDataAttributeType;
archivedObjectDescription.attributeValueClassName = @"NSData";
archivedObjectDescription.defaultValue = nil;

//Add attributes.
entityDescription.properties = @[ creationDateDescription, archivedObjectDescription ];

//Voila.
return entityDescription;
}

获取

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"EntityName"];

NSError *error;
id results = [appdel.managedObjectContext executeFetchRequest:request error:&error];
int i=100;
if ([results count])
{
for (NSManagedObject *object1 in results)
{
NSManagedObjectClass *object = (NSManagedObjectClass *)object1;
}
}

保存

        NSManagedObject   *object = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:self.managedObjectContext];
NSManagedObjectClass *polyline = (NSManagedObjectClass *)object;
polyline.json_string=request.responseString;
if (![self.managedObjectContext save:&error1]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

关于ios - 如何以编程方式添加获取的属性(到数据模型)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123069/

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