宏允许您在编译时生成重复代码。这就是视频和本文中显示的内容。
创建宏
File> New> Package
然后选择Swift宏模板
正如视频中所示,我将其称为“WWDC”
在WWDC客户端文件夹中我们得到以下代码:
import WWDC
let a = 17
let b = 25
let (result, code) = #stringify(a + b)
print("The value \(result) was produced by the code \"\(code)\"")
要展开,#stringify我们可以右键单击它并选择展开宏。然后我们可以看到完整的代码如下:
import WWDC
let a = 17
let b = 25
let (result, code) = #stringify(a + b)
print("The value \(result) was produced by the code \"\(code)\"")
该定义位于 WWDC 文件中,如下所示