gpt4 book ai didi

ios - NSFetchRequest 找不到实体名称的 NSEntityDescription

转载 作者:可可西里 更新时间:2023-11-01 03:25:38 26 4
gpt4 key购买 nike

我已经针对 SO 上的类似问题尝试了建议的解决方案,但没有成功。提取请求找不到实体名称“GarmentType”。

这是我的数据模型: enter image description here

错误是在这个帮助类中的 executeFetchRequest 抛出的:

//
// GarmentType+Create.m
// Dressy
//
// Created by Michael Mangold on 9/4/13.
// Copyright (c) 2013 Michael Mangold. All rights reserved.
//

#import "GarmentType+Create.h"

@implementation GarmentType (Create)

// Creates or fetches a Core Data GarmentType entity.
+ (GarmentType *)garmentTypeWithName:(NSString *)name inManagedObjectContext:(NSManagedObjectContext *)context
{
NSLog(@"name:%@ context:%@",name,context);
GarmentType *garmentType = nil;

// Build fetch request.
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"GarmentType"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:@"name = %@",name];

// Execute fetch requesst.
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error]; // This is where it dies.

if (!matches || [matches count] > 1) {
NSLog(@"Error creating GarmentType.");
} else if ([matches count] == 0) {
garmentType = [NSEntityDescription insertNewObjectForEntityForName:@"GarmentType" inManagedObjectContext:context];
garmentType.name = name;
} else {
garmentType = [matches lastObject];
}

return garmentType;
}

@end

名称记录了正确的名称并且上下文不是零。

当按下“完成”按钮时,从 UITableViewController IBAction 调用助手:

- (IBAction)handleDoneButton:(UIBarButtonItem *)sender
{
// Add garment type to core data.
[DressyHelper openDocumentUsingBlock:^(UIManagedDocument *document){
GarmentType *newGarment = [GarmentType garmentTypeWithName:self.garmentTypeNameTextField.text inManagedObjectContext:document.managedObjectContext];
NSLog(@"newGarment.name:%@",newGarment.name);
}];
[self dismissViewControllerAnimated:YES completion:nil];
}

newGarment.name 记录正常。

这是 GarmentType 的 .h:

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

@class Garment;

@interface GarmentType : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *garments;
@end

@interface GarmentType (CoreDataGeneratedAccessors)

- (void)addGarmentsObject:(Garment *)value;
- (void)removeGarmentsObject:(Garment *)value;
- (void)addGarments:(NSSet *)values;
- (void)removeGarments:(NSSet *)values;

@end

为代码转储道歉,但我已经为此苦苦挣扎了一段时间。更改实体名称会导致相同的错误,只是使用新名称作为未找到的实体。

GarmentType 检查器: enter image description here

异常(exception)情况:

2013-12-10 13:51:50.320 Dressy[4336:70b] name:asdf context:<NSManagedObjectContext: 0x8ad86f0>
2013-12-10 13:51:50.323 Dressy[4336:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'GarmentType''
*** First throw call stack:
(
0 CoreFoundation 0x017465e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014c98b6 objc_exception_throw + 44
2 CoreData 0x019b4b6e -[NSFetchRequest(_NSInternalMethods) _resolveEntityWithContext:] + 510
3 CoreData 0x019b34d6 -[NSManagedObjectContext executeFetchRequest:error:] + 70
4 Dressy 0x000051de +[GarmentType(Create) garmentTypeWithName:inManagedObjectContext:] + 510
5 Dressy 0x00002170 __54-[GarmentTypeAddTableViewController handleDoneButton:]_block_invoke + 208
6 Dressy 0x00006d51 __39+[DressyHelper openDocumentUsingBlock:]_block_invoke21 + 97
7 UIKit 0x00781f7b __59-[UIDocument saveToURL:forSaveOperation:completionHandler:]_block_invoke570 + 54
8 libdispatch.dylib 0x01e43440 _dispatch_barrier_sync_f_slow_invoke + 71
9 libdispatch.dylib 0x01e544b0 _dispatch_client_callout + 14
10 libdispatch.dylib 0x01e4275e _dispatch_main_queue_callback_4CF + 340
11 CoreFoundation 0x017aba5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
12 CoreFoundation 0x016ec6bb __CFRunLoopRun + 1963
13 CoreFoundation 0x016ebac3 CFRunLoopRunSpecific + 467
14 CoreFoundation 0x016eb8db CFRunLoopRunInMode + 123
15 GraphicsServices 0x03a4d9e2 GSEventRunModal + 192
16 GraphicsServices 0x03a4d809 GSEventRun + 104
17 UIKit 0x00237d3b UIApplicationMain + 1225
18 Dressy 0x0000585d main + 141
19 libdyld.dylib 0x020e670d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

TIA

最佳答案

终于找到问题了。 .xcdatamodel 未包含在目标成员资格中。在 Xcode 左侧的导航器 Pane 中选择 xcdatamodel,然后在右侧的 Utilities Pane 中选择第一个选项卡(文件检查器),然后在 Target Membership 部分中选择数据模型可以解决问题。非常感谢所有回复的人。

enter image description here

关于ios - NSFetchRequest 找不到实体名称的 NSEntityDescription,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500404/

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