问题描述
代码是否有问题?我无法使用此谓词获取任何内容.注释掉谓词,然后可以从实体"BankDetail"中获取所有对象.所以我认为问题出在这两行.
Is there something wrong with the code? I can fetch nothing using this predicate. Comment out the predicate then I can fetch all objects from entity "BankDetail". So I think problem resides in these two lines.
// self.bankInfo.name is set in prepareForSegue in first view controller
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"info.name = %@",self.bankInfo.name];
[request setPredicate:predicate];
我的模型包括两个实体,它们之间是一对一的关系
My model includes two entities, which are in one-to-one relationship
BankInfo.h
BankInfo.h
@class BankDetail;
@interface BankInfo : NSManagedObject
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) BankDetail * detail;
@end
BankDetail.h
BankDetail.h
@class BankInfo;
@interface BankDetail : NSManagedObject
@property (nonatomic, retain) NSString * closeDate;
@property (nonatomic, retain) NSString * updateDate;
@property (nonatomic, retain) NSString * zip;
@property (nonatomic, retain) NSString * acquiringInstitution;
@property (nonatomic, retain) BankInfo * info;
@end
要提供更多详细信息:
-
self.bankInfo.name
确实已设置,我在谓词行之前NSLog记录了它.
self.bankInfo.name
is definitely set, I NSLog it right before the line of predicate
而我在 viewDidLoad
中进行此操作:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BankDetail" inManagedObjectContext:context];
NSLog(@"[entity description] is %@",[entity description]);
在控制台中获取它:
info = "(<NSRelationshipDescription: 0x6d3eb30>), name info, isOptional 1, isTransient 0, entity BankDetail, renamingIdentifier info, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, destination entity BankInfo, inverseRelationship detail, minCount 1, maxCount 1, isOrdered 0, deleteRule 2";
事实证明,谓词没有任何问题.该错误是由其他地方的粗心错误引起的(请参阅接受的答案,这与重命名有关).如果您对谓词有疑问,请忽略此帖子.
Turn out there's nothing wrong with the predicate. The bug is caused by a careless mistake elsewhere (see the accepted answer, it's about renaming). Please IGNORE this post if you have question about predicate.
推荐答案
我终于解决了该错误.根本不涉及谓词.通过在Xcode编辑器中的.xcdatamodeld中,我将实体从 BankDetails
重命名为 BankDetail
,只需按回车键并进行更改即可,就像重命名任何其他文件一样.
I finally fix the bug. It's not about the predicate at all. I rename the entity from BankDetails
to BankDetail
in .xcdatamodeld in Xcode editor by simply hit return key and change it, like renaming any other file.
然后,我继续手动重命名自动生成的NSManagedObject子类文件以及引用它的其他类文件中的各个部分,直到所有警告消失为止.我想我已经重命名了所有必要的内容,但不是.该程序没有按照我在问题中所描述的那样运行,但是没有错误,没有警告,Xcode只能编译并运行.
Then I go ahead to manually rename various parts in the auto generated NSManagedObject subclass files, and in other class files that reference it, until all warnings go away. I think I have renamed all that are necessary, but I wasn't. The program doesn't run as I expect like I described in the question, but no error, no warning, Xcode just compile and run.
一段时间后,当我最终尝试重新生成子类文件以修复该错误时,旧的 BankDetails
作为类名出现,并且出现了许多命名错误.我以为一开始是Xcode的一些错误.因此,我清理了构建并再次重新生成了子类文件.但是,它仍然是旧的 BankDetails
.经过几次尝试,我在数据模型检查器中发现了问题(请参见下面的屏幕截图).更改类名称,一切运行正常.
After some time, when I finally try to regenerate the subclass files to fix the bug, the old BankDetails
shows up as class name, and lots of naming errors come out. I thought it was some bug of Xcode at first. So I clean the build and regenerate subclass files again. Yet, it's still the good old BankDetails
. After a few attempts, I found the problem in Data Model Inspector (see screenshot below). Change the Class name and everything runs perfectly.
道德:请务必格外小心!
Moral: always rename in extreme caution!
这篇关于核心数据:为什么我无法使用谓词获取任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!