Error[8]: Undefined offset: 9, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我正在编写一个LogUtil类,它只是将参数传递给’print’函数,并提供额外的信息,如“filename,line NO.”等.请参阅下面的代码. 但是,通常我们使用’print’函数输出如下: print("param1", "param2") 它输出: param1 param2 但是,使用我的LogUtil进行记录, LogUtil.d("tag", "param1", "param2" 我正在编写一个LogUtil类,它只是将参数传递给’print’函数,并提供额外的信息,如“filename,line NO.”等.请参阅下面的代码.

但是,通常我们使用’print’函数输出如下:

print("param1","param2")

它输出:

param1 param2

但是,使用我的LogUtil进行记录,

LogUtil.d("tag","param1","param2"),

它输出:

D - /LogUtil.swift(44): [["tag","param2"]]

我想输出原始的’打印’样式:

D -/LogUtil.swift(44): tag param1 param2

如何解决这个问题?谢谢!

class func p(level: LogLevel,items: Any...) {    //todo eric thread ID    //todo eric filename is too long    //todo eric loglevel control    //todo eric outputs to file    let filePath = __file__    let lineNo = __liNE__    print("\(getLogLevelDesc(level)) - \(filePath)(\(lineNo)):",items)}class func d(items: Any...) {    return p(LogLevel.DEBUG,items: items)}class func i(items: Any...) {    return p(LogLevel.INFO,items: items)}class func w(items: Any...) {    return p(LogLevel.WARN,items: items)}class func e(items: Any...) {    return p(LogLevel.ERROR,items: items)}class func fatal(items: Any...) {    return p(LogLevel.FATAL,items: items)}
解决方法 首先调用LogUtil.d(“tag”,“param1”,“param2”).由于items参数声明为variadic Any …,因此d中的局部变量项具有[Any]类型,并且具有值[“tag”,“param2”].

然后d调用p,将自己的项作为p的items参数的第一个参数传递.由于p的items参数也是可变参数,因此d可以为p的项传递更多参数.也就是说,d可以调用p(LogLevel.DEBUG,items:items,“more”,“words”).它没有,但它可以.

由于p的items参数是variadic Any …,p中的局部变量项也是[Any]类型,其值是[[“tag”,“param2”]].它是一个元素数组中的三个元素的数组.

你需要做的是make p或一些新函数,取一个非变量项参数,并从d和所有其他可变函数中调用它.

此外,不推荐使用__file__和__liNE___.如果您已升级到Xcode 7.3,则应使用新的#file和#line特殊表单.

但是你仍然有一个问题:#file和#line将返回在函数p中使用它们的文件和行.这可能不是你想要的.您可能想要程序调用d或i或其他任何内容的文件和行.

为此,您将文件和行参数传递给d(和其他函数),默认值为#file和#line.默认值将在呼叫站点展开,因此您将获得对d或i或其他任何呼叫的文件和行号.

所以最终你想要一个知道如何获取[Any](只有一层数组包装)并将其转换为空格分隔的字符串的函数.这是你如何做到这一点:

struct LogUtil {    enum LogLevel: String {        case DeBUG        case Info        case Warning        case Error        case Fatal    }    // The low-level function that all the others call.    private static func log(level level: LogLevel,file: String,line: Int,items: [Any]) {        let itemString = items.map { String(
static func p(level: LogLevel,items: Any...,file: String = #file,line: Int = #line) {        log(level: level,file: file,line: line,items: items)    }    static func d(items: Any...,line: Int = #line) {        log(level: .DeBUG,items: items)    }    static func i(items: Any...,line: Int = #line) {        log(level: .Info,items: items)    }    // other level-specific functions here...}
) }.joinWithSeparator(" ") print("\(level) - \(file)(\(line)): \(itemString)") }

请注意,log(level:file:line:items :)不是variadic,因此它不会在其项目周围添加另一个数组包装器.

然后定义所有用户可见的函数以调用日志函数:

DeBUG - /var/folders/kn/<snip>/playground282.swift(33): tag foo bar

当您调用LogUtil.d(“tag”,“foo”,“bar”)时,输出看起来像您想要的:

[+++] 总结

以上是内存溢出为你收集整理的swift – 将variadic参数传递给’print’不会产生所需的输出全部内容,希望文章能够帮你解决swift – 将variadic参数传递给’print’不会产生所需的输出所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
swift – 将variadic参数传递给’print’不会产生所需的输出_app_内存溢出

swift – 将variadic参数传递给’print’不会产生所需的输出

swift – 将variadic参数传递给’print’不会产生所需的输出,第1张

概述我正在编写一个LogUtil类,它只是将参数传递给’print’函数,并提供额外的信息,如“filename,line NO.”等.请参阅下面的代码. 但是,通常我们使用’print’函数输出如下: print("param1", "param2") 它输出: param1 param2 但是,使用我的LogUtil进行记录, LogUtil.d("tag", "param1", "param2" 我正在编写一个LogUtil类,它只是将参数传递给’print’函数,并提供额外的信息,如“filename,line NO.”等.请参阅下面的代码.

但是,通常我们使用’print’函数输出如下:

print("param1","param2")

它输出:

param1 param2

但是,使用我的LogUtil进行记录,

LogUtil.d("tag","param1","param2"),

它输出:

D - /LogUtil.swift(44): [["tag","param2"]]

我想输出原始的’打印’样式:

D -/LogUtil.swift(44): tag param1 param2

如何解决这个问题?谢谢!

class func p(level: LogLevel,items: Any...) {    //todo eric thread ID    //todo eric filename is too long    //todo eric loglevel control    //todo eric outputs to file    let filePath = __file__    let lineNo = __liNE__    print("\(getLogLevelDesc(level)) - \(filePath)(\(lineNo)):",items)}class func d(items: Any...) {    return p(LogLevel.DEBUG,items: items)}class func i(items: Any...) {    return p(LogLevel.INFO,items: items)}class func w(items: Any...) {    return p(LogLevel.WARN,items: items)}class func e(items: Any...) {    return p(LogLevel.ERROR,items: items)}class func fatal(items: Any...) {    return p(LogLevel.FATAL,items: items)}
解决方法 首先调用LogUtil.d(“tag”,“param1”,“param2”).由于items参数声明为variadic Any …,因此d中的局部变量项具有[Any]类型,并且具有值[“tag”,“param2”].

然后d调用p,将自己的项作为p的items参数的第一个参数传递.由于p的items参数也是可变参数,因此d可以为p的项传递更多参数.也就是说,d可以调用p(LogLevel.DEBUG,items:items,“more”,“words”).它没有,但它可以.

由于p的items参数是variadic Any …,p中的局部变量项也是[Any]类型,其值是[[“tag”,“param2”]].它是一个元素数组中的三个元素的数组.

你需要做的是make p或一些新函数,取一个非变量项参数,并从d和所有其他可变函数中调用它.

此外,不推荐使用__file__和__liNE___.如果您已升级到Xcode 7.3,则应使用新的#file和#line特殊表单.

但是你仍然有一个问题:#file和#line将返回在函数p中使用它们的文件和行.这可能不是你想要的.您可能想要程序调用d或i或其他任何内容的文件和行.

为此,您将文件和行参数传递给d(和其他函数),默认值为#file和#line.默认值将在呼叫站点展开,因此您将获得对d或i或其他任何呼叫的文件和行号.

所以最终你想要一个知道如何获取[Any](只有一层数组包装)并将其转换为空格分隔的字符串的函数.这是你如何做到这一点:

struct LogUtil {    enum LogLevel: String {        case DeBUG        case Info        case Warning        case Error        case Fatal    }    // The low-level function that all the others call.    private static func log(level level: LogLevel,file: String,line: Int,items: [Any]) {        let itemString = items.map { String(
static func p(level: LogLevel,items: Any...,file: String = #file,line: Int = #line) {        log(level: level,file: file,line: line,items: items)    }    static func d(items: Any...,line: Int = #line) {        log(level: .DeBUG,items: items)    }    static func i(items: Any...,line: Int = #line) {        log(level: .Info,items: items)    }    // other level-specific functions here...}
) }.joinWithSeparator(" ") print("\(level) - \(file)(\(line)): \(itemString)") }

请注意,log(level:file:line:items :)不是variadic,因此它不会在其项目周围添加另一个数组包装器.

然后定义所有用户可见的函数以调用日志函数:

DeBUG - /var/folders/kn/<snip>/playground282.swift(33): tag foo bar

当您调用LogUtil.d(“tag”,“foo”,“bar”)时,输出看起来像您想要的:

总结

以上是内存溢出为你收集整理的swift – 将variadic参数传递给’print’不会产生所需的输出全部内容,希望文章能够帮你解决swift – 将variadic参数传递给’print’不会产生所需的输出所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1016492.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存