WWDC 2023:编写 Swift 宏

宏允许您在编译时生成重复代码。这就是视频和本文中显示的内容。

创建宏

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 文件中,如下所示࿱

猜你喜欢

转载自blog.csdn.net/iCloudEnd/article/details/131337392
今日推荐