如果这有点含糊,我们深表歉意,但这只是问题的一半。
我有一个正在开发的基于文档的核心数据应用程序,它在运行时按照指示进行操作,不会产生任何错误。但是当用户保存文档时,文档弹出“文档“xyz”无法保存为“xyz”。发生多个验证错误”警报。
我的问题是 - 您从哪里着手解决/调试这个问题?由于程序没有在调试器中失败,我没有堆栈跟踪等。这可能是错误的实体关系,还是没有数据保存在实体的非可选属性中,或者......有没有办法准确说明什么是验证失败?
非常感谢任何有关最佳进行方式的建议。
与此相关,将来捕获此类错误的最佳方法是什么/如何,这样它就不会影响到用户。
非常感谢
最佳答案
好的,按照 TechZen 的建议,捕获保存操作中的错误。将以下内容添加到 MyDocument.m
- (NSError *)willPresentError:(NSError *)error {
// Only deal with Core Data Errors
if (!([[error domain] isEqualToString:NSCocoaErrorDomain])) {
return error;
}
NSInteger errorCode = [error code];
if ((errorCode < NSValidationErrorMinimum) || (errorCode > NSValidationErrorMaximum)) {
return error;
}
// If there is only 1 error, let the usual alert display it
if (errorCode != NSValidationMultipleErrorsError) {
return error;
}
// Get the errors. NSValidationMultipleErrorsError - the errors are in an array in the userInfo dictionary for key NSDetailedErrorsKey
NSArray *detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
NSUInteger errorCount = [detailedErrors count];
NSMutableString *errorString = [NSMutableString stringWithFormat:@"There are %lu validation errors:-", errorCount];
for (int i = 0; i < errorCount; i++) {
[errorString appendFormat:@"%@\n",
[[detailedErrors objectAtIndex:i] localizedDescription]];
}
// Create a new error with the new userInfo and return it
NSMutableDictionary *newUserInfo = [NSMutableDictionary dictionaryWithDictionary:[error userInfo]];
[newUserInfo setObject:errorString forKey:NSLocalizedDescriptionKey];
NSError *newError = [NSError errorWithDomain:[error domain] code:[error code] userInfo:newUserInfo];
return newError;
}
请注意,如果有 100 个错误,那么您将收到包含 100 个不是最佳项目的警报,但这是处理保存错误的良好起点。
关于macos - 无法保存文档 "xyz"。出现多个验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5930784/