在这种情况下如何确定是否发生错误(Objective-C)
嘿伙计们,我有这个函数:
//retrieves checksum within core data store, returns 0 if a checksum has not been stored
-(double)getStoredChecksum {
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DataFeedManager" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
//+for (NSManagedObject* item in fetchedObjects) //REMOVE
//+[[self managedObjectContext] deleteObject:item]; //REMOVE FOR CLEARING CHECKSUM CONTENTS
if ([fetchedObjects count] == 0){ //array is empty
NSLog(@"FETCHED OBJECTS IS EMPTY");
checksumStored = NO;
return 0;
}
if (fetchedObjects == nil) {
NSLog(@"Error while fetching\n%@",
([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
}
if ([fetchedObjects count] > 1)
NSLog(@"====BAD:Multiple Checksums Retrieved!!!!====");
checksumStored = YES;
double returnable = [[[fetchedObjects objectAtIndex:0] valueForKey:@"lastUpdateCheckSum"] doubleValue];
return returnable;
}//getStoredChecksum()
如您所见,它执行一个获取请求,但是我不确定如何检查是否发生错误。正如你所看到的,我通过 [fetchedObjects count] == 0
检查数组是否为空,但是,我听说如果发生错误并且数组为零,则返回 0 count 也是如此,因此以这种方式测试数组是否为空是安全的,但它掩盖了我对空数组的检查。因此,问题是我没有确定的方法来检查是否发生错误。关于我应该如何去做这件事有什么想法吗?
提前致谢!
编辑:
在这种情况下我将如何检查错误?
NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];
由于 NSUInteger 被指定为无符号整数,因此我什至无法为其分配 -1 值并在以后检查 -1(如果我愿意)。
Hey guys, I have this function:
//retrieves checksum within core data store, returns 0 if a checksum has not been stored
-(double)getStoredChecksum {
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DataFeedManager" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
//+for (NSManagedObject* item in fetchedObjects) //REMOVE
//+[[self managedObjectContext] deleteObject:item]; //REMOVE FOR CLEARING CHECKSUM CONTENTS
if ([fetchedObjects count] == 0){ //array is empty
NSLog(@"FETCHED OBJECTS IS EMPTY");
checksumStored = NO;
return 0;
}
if (fetchedObjects == nil) {
NSLog(@"Error while fetching\n%@",
([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
}
if ([fetchedObjects count] > 1)
NSLog(@"====BAD:Multiple Checksums Retrieved!!!!====");
checksumStored = YES;
double returnable = [[[fetchedObjects objectAtIndex:0] valueForKey:@"lastUpdateCheckSum"] doubleValue];
return returnable;
}//getStoredChecksum()
As you can see, it executes a fetch request, however I am unsure of how to check if an error occurred. As you can see, I am checking to see if the array is empty via [fetchedObjects count] == 0
however, I heard somewhere that if the error occurs and the array is nil, 0 is returned for count as well, thus it is safe to test for array emptiness that way, but it masks my check for a null array. Thus the problem is that I have no definitive way of checking if an error occurred. Any thoughts on how I should go about doing this?
Thanks in advance!
EDIT:
How would I check for an error in this case?
NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];
As NSUInteger is specified as an unsigned int, so I can't even assign it a value of -1 and check for a -1 later if I wanted to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用executeFetchRequest::
之前将error
设置为nil
。如果error
不是nil
,那么它被设置,因此发生错误。count
检查的原因是屏蔽 < code>nil 检查是因为发送到nil
的任何消息都会返回nil
,在本例中也是 0。通过检查可以轻松解决此问题首先 nil
,或者修改您的if
语句,如下所示:对于
countForFetchRequest::
的情况,返回NSNotFound
发生错误,因此只需检查该错误即可。Seterror
tonil
before callingexecuteFetchRequest::
. Iferror
is notnil
after then it was set and thus an error occured.The reason the
count
check is masking thenil
check is because any message sent tonil
returnsnil
which in this case is also 0. This can be easily fixed by checking fornil
first, or modify yourif
statement so it is as followed:For the case with
countForFetchRequest::
NSNotFound
is returned when an error occurs, so just check for that.复制&从
countForFetchRequest:error:
的文档中粘贴所以,
Copy & pasted from the docs for
countForFetchRequest:error:
So,