核心数据是否在后台进行自己的类型转换?
我正在对两个列表进行简单比较,以查看“评估”列表中的哪些项目包含在更大的“目标”列表中。我通过解析两个 CSV 文件并将所有内容存储为字符串来即时获取数据。我成功地将数据导入到数据存储中,并且可以毫无问题地获取实体列表
。当我实际进行搜索时,问题就出现了。本质上,我正在从目标列表中的评估列表中寻找 1234
形式的短 ISBN,其形式为 1234-5
。我使用的谓词是我正在使用 CONTAINS
比较的形式 [NSString stringWithFormat:@"%@ CONTAINS %@", kOC_Target_PrintBookCode, evalIsbn]
错误我get 如下(由我的 NSLog
抓取)
NSInvalidArgumentException: Can't look for value (1494) in string (49885); value is not a string
我的印象是,即使 ISBN 是从 NSString 读取的,并且核心数据存储将数据点指定为字符串,但核心无论出于何种原因,数据仍然在后台利用其价值执行某些操作。有什么想法吗?
这是相关的处理逻辑(尽管我不确定地使用该术语)代码。除非代码中另有说明,否则所有被操作和/或存储的值都是 NSString
:
NSArray *evalBooks = [self getEntitiesByName:kOC_EntityName_EvalBook
usingPredicateValue:[NSString stringWithFormat:@"%@ > \"\"", kOC_Eval_Bookcode]
withSubstitutionVariables:nil
inModel:[self managedObjectModel]
andContext:[self managedObjectContext]
sortByAttribute:nil];
if ( ( !evalBooks ) || ( [evalBooks count] == 0 ) ) {
// we have problem
NSLog(@"( !evalBooks ) || ( [evalBooks count] == 0 )");
return;
}
[evalBooks retain];
int firstEvalBook = 0;
int thisEvalBook = firstEvalBook;
int lastEvalBook = [evalBooks count]; NSLog(@"lastEvalBook: %i", lastEvalBook);
for (thisEvalBook = firstEvalBook; thisEvalBook < lastEvalBook; thisEvalBook++) {
NSManagedObject *evalBook = [[evalBooks objectAtIndex:thisEvalBook] retain];
NSString *rawIsbn = [[evalBook valueForKey:kOC_Eval_Bookcode] retain];
NSString *isbnRoot = [[self getIsbnRootFromIsbn:rawIsbn] retain];
// this is a custom method I created and use elsewhere without any issues.
NSArray *foundBooks = [self getEntitiesByName:kOC_EntityName_TargetBook
usingPredicateValue:[NSString stringWithFormat:@"%@ CONTAINS %@", kOC_Target_PrintBookCode, isbnRoot]
withSubstitutionVariables:nil
inModel:[self managedObjectModel]
andContext:[self managedObjectContext]
sortByAttribute:kOC_Target_PrintBookCode];
if ( foundBooks != nil ) {
[foundBooks retain];
NSLog(@"foundBooks: %lu", [foundBooks count]);
} else {
}
I am working on a simple comparison of two lists to see which items in an "evaluation" list are contained in a larger "target" list. I am getting the data on-the-fly- by parsing two CSV files and storing everything as strings. I successfully import the data into the data store and I can get a list of entities no problem
The problem comes when I actually do a search. Essentially, I am looking for short ISBNs in the form of 1234
from the evaluation list in the target list, which are in the form of 1234-5
. The predicate I am using is I am using the CONTAINS
comparison in the form of [NSString stringWithFormat:@"%@ CONTAINS %@", kOC_Target_PrintBookCode, evalIsbn]
The error I get is the following (grabbed by my NSLog
)
NSInvalidArgumentException: Can't look for value (1494) in string (49885); value is not a string
I get the impression that even though the ISBN is being read from a NSString and the Core Data store has the data point spec'd as a String, that Core Data is still doing something in the background with the value for whatever reason it sees fit. Any ideas?
Here is the relevant process logic (though I use that term dubiously) code. Unless otherwise noted in the code, all values being manipulated and/or stored are NSString
:
NSArray *evalBooks = [self getEntitiesByName:kOC_EntityName_EvalBook
usingPredicateValue:[NSString stringWithFormat:@"%@ > \"\"", kOC_Eval_Bookcode]
withSubstitutionVariables:nil
inModel:[self managedObjectModel]
andContext:[self managedObjectContext]
sortByAttribute:nil];
if ( ( !evalBooks ) || ( [evalBooks count] == 0 ) ) {
// we have problem
NSLog(@"( !evalBooks ) || ( [evalBooks count] == 0 )");
return;
}
[evalBooks retain];
int firstEvalBook = 0;
int thisEvalBook = firstEvalBook;
int lastEvalBook = [evalBooks count]; NSLog(@"lastEvalBook: %i", lastEvalBook);
for (thisEvalBook = firstEvalBook; thisEvalBook < lastEvalBook; thisEvalBook++) {
NSManagedObject *evalBook = [[evalBooks objectAtIndex:thisEvalBook] retain];
NSString *rawIsbn = [[evalBook valueForKey:kOC_Eval_Bookcode] retain];
NSString *isbnRoot = [[self getIsbnRootFromIsbn:rawIsbn] retain];
// this is a custom method I created and use elsewhere without any issues.
NSArray *foundBooks = [self getEntitiesByName:kOC_EntityName_TargetBook
usingPredicateValue:[NSString stringWithFormat:@"%@ CONTAINS %@", kOC_Target_PrintBookCode, isbnRoot]
withSubstitutionVariables:nil
inModel:[self managedObjectModel]
andContext:[self managedObjectContext]
sortByAttribute:kOC_Target_PrintBookCode];
if ( foundBooks != nil ) {
[foundBooks retain];
NSLog(@"foundBooks: %lu", [foundBooks count]);
} else {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将谓词构建为
NSString
,我相信实际上应该是
似乎您混淆了
predicateWithFormat:
的工作方式与stringWithFormat:
有效。If you're building your predicate as an
NSString
, I believeshould actually be
It seems that you're confusing the way
predicateWithFormat:
works with the waystringWithFormat:
works.据推测,
kOC_Target_PrintBookCode
或isbnRoot
都不是可以转换为字符串的对象。例如,如果其中一个是整数,则%@
运算符无法将整数转换为字符串值。Presumably either
kOC_Target_PrintBookCode
orisbnRoot
is not an object that can be converted to a string. E.g. if either is an integer, the%@
operator cannot convert the integer to a string value.