xcode OSX/Cocoa 的错误代码参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:23:48  来源:igfitidea点击:

Error Code Reference for OSX/Cocoa

cocoaxcodemacos

提问by Kristopher Johnson

If I get an error code result from a Cocoa function, is there any easy way to figure out what it means (other than by grepping through all the .h files in the framework bundles)?

如果我从 Cocoa 函数中得到错误代码结果,有没有什么简单的方法可以弄清楚它的含义(除了通过 grepping 框架包中的所有 .h 文件)?

回答by Chris Hanson

You should look at the <Framework/FrameworkErrors.h>header for whatever framework the method you're using that's returning an error comes from.

您应该查看<Framework/FrameworkErrors.h>您正在使用的返回错误的方法来自的任何框架的标头。

For example, an NSErrorin the Cocoa domain that you get from a method in the Foundation framework will have its codeproperty described in the <Foundation/FoundationErrors.h>header. Similarly with AppKit and <AppKit/AppKitErrors.h>and Core Data and <CoreData/CoreDataErrors.h>.

例如,NSError您从 Foundation 框架中的方法获得的 Cocoa 域中的code将在<Foundation/FoundationErrors.h>标头中描述其属性。与 AppKit 和<AppKit/AppKitErrors.h>Core Data 和<CoreData/CoreDataErrors.h>.

Also, if you print the description of the NSErrorin the debugger, it should include not only the error domain and code, but also the name of the actual error code constant so you can look it up in the API reference.

此外,如果您NSError在调试器中打印 的描述,它不仅应包含错误域和代码,还应包含实际错误代码常量的名称,以便您可以在 API 参考中查找。

回答by Mark Amery

The sections on 'Error Domains' and 'Error Codes' in Apple's Error Handling Programming Guideaddress this reasonably well. You need to do the following:

Apple 的错误处理编程指南中的“错误域”和“错误代码”部分很好地解决了这个问题。您需要执行以下操作:

  1. Log the error, taking note of both the error domain(a human-readable / Googleable string that tells you where to look for the error code definitions) and the error codeitself (an integer)

  2. Sniff around on Google (or read from the list below) and figure out the name of the header file(s) where the error codes for that error domain are defined

  3. Search those header file(s) for the error code you got. You should find both a constant name for the error code (like ENOMEM), and hopefully also an explanatory comment (like /* Cannot allocate memory */) explaining what the error means. If there's no comment, and the constant name isn't self-explanatory, just Google the constant name and you'll probably find a proper description.

  1. 记录错误,同时记下错误域(一个人类可读/可搜索的字符串,告诉您在哪里查找错误代码定义)和错误代码本身(一个整数)

  2. 在 Google 上嗅探(或从下面的列表中阅读)并找出定义了该错误域的错误代码的头文件的名称

  3. 在这些头文件中搜索您得到的错误代码。您应该找到错误代码的常量名称(如ENOMEM),并希望还找到解释/* Cannot allocate memory */错误含义的解释性注释(如)。如果没有评论,并且常量名称不是不言自明的,只需在 Google 上搜索常量名称,您可能会找到正确的描述。

Some header files of major error domains:

主要错误域的一些头文件:

NSCocoaErrorDomain

NSCocoaErrorDomain

Error code declarations are spread across three header files:

错误代码声明分布在三个头文件中:

  • <Foundation/FoundationErrors.h>(Generic Foundation error codes)
  • <AppKit/AppKitErrors.h>(Generic AppKit error codes)
  • <CoreData/CoreDataErrors.h>(Core Data error codes)
  • <Foundation/FoundationErrors.h>(通用基金会错误代码)
  • <AppKit/AppKitErrors.h>(通用 AppKit 错误代码)
  • <CoreData/CoreDataErrors.h>(核心数据错误代码)

NSURLErrorDomain

NSURLErrorDomain

Check NSURLError.h

查看 NSURLError.h

NSXMLParserErrorDomain

NSXMLParserErrorDomain

CheckNSXMLParser.h

查看NSXMLParser.h

NSMachErrorDomain

NSMachErrorDomain

Check /usr/include/mach/kern_return.h

查看 /usr/include/mach/kern_return.h

NSPOSIXErrorDomain

NSPOSIXErrorDomain

Check /usr/include/sys/errno.h

查看 /usr/include/sys/errno.h

NSOSStatusErrorDomain

NSOSStatusErrorDomain

Check

查看

/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MacErrors.h

回答by Mark Amery

Also, Cocoa's NSError is meant to be displayable to the end user. If you just log it, it should be readable.

此外,Cocoa 的 NSError 旨在向最终用户显示。如果你只是记录它,它应该是可读的。

If you're talking about Carbon's OSStatus and such, MacErrors.h.

如果你在谈论 Carbon 的 OSStatus 等,MacErrors.h。

回答by Will

For NSError errors add a line of code:

对于 NSError 错误添加一行代码:

NSError     *error;

// ... Some code that returns an error

// Get the error as a string
NSString *s = [error localizedDescription];

// Observe the code for yourself or display to the user.