作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
接下来是我可能会想到的所有背景,以尝试找出错误的来源。我当然会提供其他可能有用的信息。在此先感谢您的帮助。
我是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"];
#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;
- (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];
}
+(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/
我是一名优秀的程序员,十分优秀!