gpt4 book ai didi

iPhone核心数据 "Production"错误处理

转载 作者:行者123 更新时间:2023-12-03 18:08:38 24 4
gpt4 key购买 nike

我在 Apple 提供的示例代码中看到了如何处理核心数据错误的引用。即:

NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.

abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

但从来没有任何应该如何实现它的示例。

是否有人拥有(或可以为我指明方向)一些实际的“生产”代码来说明上述方法。

最佳答案

这是我想出的一种通用方法,用于在 iPhone 上处理和显示验证错误。但马库斯是对的:您可能希望调整消息以使其更加用户友好。但这至少为您提供了一个起点,了解哪些字段未验证以及原因。

- (void)displayValidationError:(NSError *)anError {
if (anError && [[anError domain] isEqualToString:@"NSCocoaErrorDomain"]) {
NSArray *errors = nil;

// multiple errors?
if ([anError code] == NSValidationMultipleErrorsError) {
errors = [[anError userInfo] objectForKey:NSDetailedErrorsKey];
} else {
errors = [NSArray arrayWithObject:anError];
}

if (errors && [errors count] > 0) {
NSString *messages = @"Reason(s):\n";

for (NSError * error in errors) {
NSString *entityName = [[[[error userInfo] objectForKey:@"NSValidationErrorObject"] entity] name];
NSString *attributeName = [[error userInfo] objectForKey:@"NSValidationErrorKey"];
NSString *msg;
switch ([error code]) {
case NSManagedObjectValidationError:
msg = @"Generic validation error.";
break;
case NSValidationMissingMandatoryPropertyError:
msg = [NSString stringWithFormat:@"The attribute '%@' mustn't be empty.", attributeName];
break;
case NSValidationRelationshipLacksMinimumCountError:
msg = [NSString stringWithFormat:@"The relationship '%@' doesn't have enough entries.", attributeName];
break;
case NSValidationRelationshipExceedsMaximumCountError:
msg = [NSString stringWithFormat:@"The relationship '%@' has too many entries.", attributeName];
break;
case NSValidationRelationshipDeniedDeleteError:
msg = [NSString stringWithFormat:@"To delete, the relationship '%@' must be empty.", attributeName];
break;
case NSValidationNumberTooLargeError:
msg = [NSString stringWithFormat:@"The number of the attribute '%@' is too large.", attributeName];
break;
case NSValidationNumberTooSmallError:
msg = [NSString stringWithFormat:@"The number of the attribute '%@' is too small.", attributeName];
break;
case NSValidationDateTooLateError:
msg = [NSString stringWithFormat:@"The date of the attribute '%@' is too late.", attributeName];
break;
case NSValidationDateTooSoonError:
msg = [NSString stringWithFormat:@"The date of the attribute '%@' is too soon.", attributeName];
break;
case NSValidationInvalidDateError:
msg = [NSString stringWithFormat:@"The date of the attribute '%@' is invalid.", attributeName];
break;
case NSValidationStringTooLongError:
msg = [NSString stringWithFormat:@"The text of the attribute '%@' is too long.", attributeName];
break;
case NSValidationStringTooShortError:
msg = [NSString stringWithFormat:@"The text of the attribute '%@' is too short.", attributeName];
break;
case NSValidationStringPatternMatchingError:
msg = [NSString stringWithFormat:@"The text of the attribute '%@' doesn't match the required pattern.", attributeName];
break;
default:
msg = [NSString stringWithFormat:@"Unknown error (code %i).", [error code]];
break;
}

messages = [messages stringByAppendingFormat:@"%@%@%@\n", (entityName?:@""),(entityName?@": ":@""),msg];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Validation Error"
message:messages
delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
}
}

享受吧。

关于iPhone核心数据 "Production"错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2262704/

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