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

查看:104
本文介绍了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函数的作用.

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

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

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

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