gpt4 book ai didi

core-data - 我该如何解决该错误:实体GroceryItems不兼容键 “hasLocations”的键值编码

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

接下来是我可能会想到的所有背景,以尝试找出错误的来源。我当然会提供其他可能有用的信息。在此先感谢您的帮助。

我是Core Data的新手,并且我有一个名为GroceryItem的实体。它具有一个名为hasLocations的多对多关系,我正尝试使用以下代码进行查询:

itemObject = (GroceryItem *)[GroceryItem itemNameToObject:itemName];

if (itemObject != nil)
{
NSLog(@"%@", [NSString stringWithFormat:@"initAndFillItemLocationsTable got an item named %@", itemObject.name]);
}

if (itemLocations != nil)
{
[itemLocations removeAllObjects];
}

if (itemObject != nil)
{
NSLog(@"Before mutable set assignment");

NSMutableSet *mutableLocationsSet = [itemObject mutableSetValueForKeyPath:@"hasLocations"];

我在输出中看到“可变集分配之前”消息,随后是

2013-01-23 03:27:14.898杂货店经理[6431:11603] initAndFillItemLocationsTable获得了一个名为Cream Cheese的商品
2013-01-23 03:27:14.899杂货店经理[6431:11603]可变集合分配之前
2013-01-23 03:27:14.901 Grocery Manager [6431:11603] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:实体GroceryItems不是与键值兼容的键值“hasLocations”。”
*
首先抛出调用堆栈:
(0x15eb012 0x1410e7e 0x1673fb1 0x11c304 0xe298db 0xb8374 0xe298db 0xec3180 0xec31de 0x17450 0xaa5c 0x439817 0x439882 0x439b2a 0x450ef5 0x450fdb 0x451286 0x451381 0x451eab 0x4524a3 0x452098 0x7adda3 0x79fad9 0x79fb54 0x407899 0x407b3d 0xe0ee83 0x15aa376 0x15a9e06 0x1591a82 0x1590f44 0x1590e1b 0x24377e3 0x2437668 0x35865c 0x27bd 0x26e5)
libc++ abi.dylib:终止调用引发异常

GroceryItem类定义的内容为:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface GroceryItem : NSManagedObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSSet *hasLocations;
@property (nonatomic, retain) NSSet *containedInIngredients;

@end

@interface GroceryItem (CoreDataGeneratedAccessors)

- (void)addHasLocationsObject:(NSManagedObject *)value;
- (void)removeHasLocationsObject:(NSManagedObject *)value;
- (void)addHasLocations:(NSSet *)values;
- (void)removeHasLocations:(NSSet *)values;

- (void)addContainedInIngredientsObject:(NSManagedObject *)value;
- (void)removeContainedInIngredientsObject:(NSManagedObject *)value;
- (void)addContainedInIngredients:(NSSet *)values;
- (void)removeContainedInIngredients:(NSSet *)values;

+(GroceryItem *)itemNameToObject:(NSString *)itemName;

我已经在数据库编辑器中验证了类GroceryItem已分配给实体GroceryItem。

我阅读了《键值编码编程指南》,并在GroceryItem.m中实现了以下代码:
- (void)addHasLocationsObject:(NSManagedObject *)value
{
[self.hasLocations setByAddingObject:value];
}

- (void)removeHasLocationsObject:(NSManagedObject *)value
{
NSMutableSet *mutable = [NSMutableSet setWithSet:self.hasLocations];

[mutable removeObject:value];

self.hasLocations = mutable;
}

- (void)addHasLocations:(NSSet *)values
{
[self.hasLocations setByAddingObjectsFromSet:values];
}

- (void)removeHasLocations:(NSSet *)values
{
NSMutableSet *mutable = [NSMutableSet setWithSet:self.hasLocations];

for (id obj in [mutable allObjects])
{
if ([values containsObject:obj])
{
[mutable removeObject: obj];
}
}
self.hasLocations = mutable;
}

- (NSUInteger)countOfHasLocations
{
return [self.hasLocations count];
}

- (NSEnumerator *)enumeratorOfHasLocations
{
return [self.hasLocations objectEnumerator];
}

- (GroceryLocation *)memberOfHasLocations:(GroceryLocation *)anObject
{
return [self.hasLocations member:anObject];

}

静态itemNameToObject函数如下所示:
+(GroceryItem *)itemNameToObject:(NSString *)itemName
{
GroceryItem *groceryItem;

groceryItem = nil;

if (itemName != nil)
{
GroceryManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"GroceryItems" inManagedObjectContext:context]];

[request setPredicate:[NSPredicate predicateWithFormat:@"name == %@", itemName]];

NSError *error;
NSArray *groceryItemObjects = [context executeFetchRequest:request error:&error];

NSInteger countOfGroceryItems = [groceryItemObjects count];

if (countOfGroceryItems == 1)
{
groceryItem = (GroceryItem *)[groceryItemObjects objectAtIndex:0];
}
}

return groceryItem;
}

最佳答案

(来自注释:)崩溃是由于该实体被声明为“GroceryItems”而引起的,但是“hasLocations”是“GroceryItem”类的属性。提取请求返回“GroceryItems”对象数组,这些对象不响应“hasLocations”方法。

同样,通常不需要实现Core Data访问器方法,它们是在运行时动态创建的。

关于core-data - 我该如何解决该错误:实体GroceryItems不兼容键 “hasLocations”的键值编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479388/

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