将NSLocalizedRecoveryOptionsErrorKey设置为NSError UserInfo不提供任何恢复选项

时间:2016-05-11 11:07:57

标签: objective-c swift macos cocoa

尝试通过presentError设置一些自定义按钮。 NSLocalizedRecoveryOptionsErrorKey无效。仅显示标准的“确定”按钮。

NSApp.presentError(NSError(domain: "", code: 0, userInfo: [
    NSLocalizedRecoveryOptionsErrorKey: ["Foo", "Bar"]
]))

设置任何其他UserInfo键也没有任何效果。

OS X 10.11是否完全不再支持它?

1 个答案:

答案 0 :(得分:2)

enter image description here

NSError *error = [NSError errorWithDomain:@"MyCoolDomain" code:1 userInfo:@{
                          NSLocalizedDescriptionKey: @"Could Not Post Message",
                          NSLocalizedFailureReasonErrorKey: @"We could not reach the XING server. Your internet connection seems to be offline.",
                          NSLocalizedRecoverySuggestionErrorKey: @"You may try again when you are online again.",
                          NSLocalizedRecoveryOptionsErrorKey: @[@"Try again", @"Cancel"]
                      }];
    NSAlert *alert = [NSAlert alertWithError:error];
    alert.informativeText = [error.localizedFailureReason stringByAppendingFormat:@"\n\n%@", alert.informativeText];
    [alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
    }];

试试这个。 来源:https://github.com/Hotel-Reservation-Service/HRSCustomErrorHandling/issues/8

当我遇到这类问题时,我会searching转到其他人的代码/问题。