gpt4 book ai didi

ios - 你如何从 Swift 向 os_log 传递错误?

转载 作者:行者123 更新时间:2023-11-28 23:19:52 24 4
gpt4 key购买 nike

我正在尝试记录来自 catch block 的错误。文档说明如下:

To format a log message, use a standard NSString or printf format string

In addition to standard format string specifiers, such as %@ and %d, the logging system supports custom decoding of values by denoting value types inline in the format %{value_type}d. In addition, the specifier %.*P can be used to decode arbitrary binary data. The system includes a number of built-in value type decoders, shown in Table 3.

errno %{errno}d Broken pipe

当我这样做时,我得到一个错误:

import os.log

do {
try throwError()
} catch {
os_log("Error: %{errno}d", log: .default, type: .error, error)
}

但是编译器输出错误:

Argument type 'Error' does not conform to expected type 'CVarArg'

有没有比将 error.localizedDescription 作为参数传递更好的方法?

最佳答案

使用@代替d并将错误转换成字符串

os_log("Error: %@", log: .default, type: .error, String(describing: error))

关于ios - 你如何从 Swift 向 os_log 传递错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59792551/

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