简体   繁体   English

有没有办法获得 CFStreamError 的字符串描述?

[英]Is there some way to get a string description of CFStreamError?

I am using an API which has handed me a CFStreamError (which is supposedly deprecated, but Apple themselves obviously don't care.)我正在使用一个 API,它给了我一个CFStreamError (据说已经过时了,但 Apple 自己显然不在乎。)

I know some of the values and I could certainly write multiple nested switch statements to convert all the values I know into strings, but there will be values I don't know.我知道一些值,我当然可以编写多个嵌套的 switch 语句来将我知道的所有值转换为字符串,但是会有一些我不知道的值。

Isn't there some convenient way to get an error message out?是不是有一些方便的方法来获取错误消息? I don't care whether it's localised or not because it will only end up in our logs anyway.我不在乎它是否已本地化,因为无论如何它最终只会出现在我们的日志中。

The 'old', pre- NSError way of handling errors usually involved return codes that were supposed to be used internally by the application (ie the developer) and not for presenting to the user.处理错误的“旧”、前NSError方法通常涉及返回代码,这些代码应该由应用程序(即开发人员)内部使用,而不是呈现给用户。

With newer APIs the NSError returned actually contains information that an be presented to the user (if appropriate).对于较新的 API,返回的NSError实际上包含呈现给用户的信息(如果合适)。

As for the CFStreamError -至于CFStreamError -
There's an entry on CocoaDev on making CFStreamError human-readable: CocoaDev 上有一个关于使CFStreamError人类可读的条目:

http://cocoadev.com/CFStreamErrorCodes http://cocoadev.com/CFStreamErrorCodes

Basically it involves manually checking the various error domains from CFStream Error Domain Constants .基本上它涉及从CFStream Error Domain Constants手动检查各种错误域。

A bit more information from Developer Technical Support can be found in this post on the Macnetworkprog mailing list.可以在 Macnetworkprog 邮件列表上的这篇文章中找到来自开发人员技术支持的更多信息。

This works for POSIX domain errors:这适用于 POSIX 域错误:

            if (err.domain == kCFStreamErrorDomainPOSIX) {
                DLog("POSIX err: %s", strerror(err.error));
            } else {
                DLog("domain: %d, value: %d", err.domain, err.error);
            }

Eg:例如:

2020-01-31 09:58:02.996603-0800 blah void CFWriteStreamCB(CFWriteStreamRef _Null_unspecified, CFStreamEventType, void * _Null_unspecified):26 POSIX err: Operation timed out 2020-01-31 09:58:02.996603-0800 blah void CFWriteStreamCB(CFWriteStreamRef _Null_unspecified, CFStreamEventType, void * _Null_unspecified):26 POSIX 错误:操作超时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM