Swift 中的宏? [英] Macros in Swift?

查看:30
本文介绍了Swift 中的宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 目前是否支持宏,或者是否有未来计划增加支持?目前我正在分散:

Does Swift currently support macros, or are there future plans to add support? Currently I'm scattering:

Log.trace(nil, function: __FUNCTION__, file: __FILE__, line: __LINE__)

在我的代码中的各个地方.

In various places throughout my code.

推荐答案

在这种情况下,您应该为宏"参数添加一个默认值.

In this case you should add a default value for the "macro" parameters.

Swift 2.2 及更高版本

func log(message: String,
        function: String = #function,
            file: String = #file,
            line: Int = #line) {

     print("Message "(message)" (File: (file), Function: (function), Line: (line))")
}

log("Some message")

Swift 2.1 及更低版本

func log(message: String,
        function: String = __FUNCTION__,
        file: String = __FILE__,
        line: Int = __LINE__) {

    print("Message "(message)" (File: (file.lastPathComponent), Function: (function), Line: (line))")
}

log("Some message")

这就是 fatalErrorassert 函数的作用.

This is what fatalError and assert functions do.

除了另一个答案中已经提到的条件编译之外,没有其他宏.

There are no other macros except the conditional compilation already mentioned in another answer.

这篇关于Swift 中的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆