gpt4 book ai didi

ios - 自定义 NSError 代码

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:25 25 4
gpt4 key购买 nike

我正在构建一个 Cocoa Touch Framework,它使用自定义 NSError 对象将错误反馈给调用者。我需要声明一个定义自定义错误代码的枚举。我去了这个Apple doc其中列出了如下所示的 NSURLErrorDomain 错误代码:

enum {    
NSURLErrorUnknown = -1,
NSURLErrorCancelled = -999,
NSURLErrorBadURL = -1000,
NSURLErrorTimedOut = -1001,
NSURLErrorUnsupportedURL = -1002,
NSURLErrorUnsupportedURL = -1002,
NSURLErrorCannotConnectToHost = -1004,
NSURLErrorDataLengthExceedsMaximum = -1103,
NSURLErrorNetworkConnectionLost = -1005,
NSURLErrorDNSLookupFailed = -1006,
NSURLErrorHTTPTooManyRedirects = -1007,
NSURLErrorResourceUnavailable = -1008,
NSURLErrorNotConnectedToInternet = -1009,
NSURLErrorRedirectToNonExistentLocation = -1010,
NSURLErrorBadServerResponse = -1011,
NSURLErrorUserCancelledAuthentication = -1012,
NSURLErrorUserAuthenticationRequired = -1013,
NSURLErrorZeroByteResource = -1014,
NSURLErrorCannotDecodeRawData = -1015,
NSURLErrorCannotDecodeContentData = -1016,
NSURLErrorCannotParseResponse = -1017,
NSURLErrorInternationalRoamingOff = -1018,
NSURLErrorCallIsActive = -1019,
NSURLErrorDataNotAllowed = -1020,
NSURLErrorRequestBodyStreamExhausted = -1021,
NSURLErrorFileDoesNotExist = -1100,
NSURLErrorFileIsDirectory = -1101,
NSURLErrorNoPermissionsToReadFile = -1102,
NSURLErrorSecureConnectionFailed = -1200,
NSURLErrorServerCertificateHasBadDate = -1201,
NSURLErrorServerCertificateUntrusted = -1202,
NSURLErrorServerCertificateHasUnknownRoot = -1203,
NSURLErrorServerCertificateNotYetValid = -1204,
NSURLErrorClientCertificateRejected = -1205,
NSURLErrorClientCertificateRequired = -1206,
NSURLErrorCannotLoadFromNetwork = -2000,
NSURLErrorCannotCreateFile= -3000,
NSURLErrorCannotOpenFile = -3001,
NSURLErrorCannotCloseFile = -3002,
NSURLErrorCannotWriteToFile = -3003,
NSURLErrorCannotRemoveFile = -3004,
NSURLErrorCannotMoveFile = -3005,
NSURLErrorDownloadDecodingFailedMidStream = -3006,
NSURLErrorDownloadDecodingFailedToComplete = -3007
}

问题 1:为什么 Apple 使用负值作为错误代码?有什么具体原因吗?

问题 2:Apple 是否遵循任何模式来随机化错误代码?

最佳答案

  1. 因为基于返回码,0 通常是可以的。剩下正整数和负整数。对成功代码(带有附加信息)使用正整数,对错误代码使用负值是 C 语言的普遍偏好。
  2. 如您所见,错误代码已分组。未知和取消是特殊的,1000 是 HTTP 错误,1100 是访问错误,1200 是连接和证书错误,2000 是网络,3000 是文件和流问题。

根据问题区域对错误进行分组,并使用负数作为返回代码(因为返回负数通常会被解释为错误)。

关于ios - 自定义 NSError 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29940689/

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