iOS 15 AttributeContainer 函数链如何工作?

mat*_*att 7 nsattributedstring swift ios15

iOS 15 中的新增功能,我们可以像这样形成 Swift AttributedString:

var att = AttributedString("Howdy")
att.font = UIFont(name:"Arial-BoldMT", size:15)
att.foregroundColor = UIColor(red:0.251, green:0.000, blue:0.502, alpha:1)
print(att)
Run Code Online (Sandbox Code Playgroud)

很酷,但是还有另一种方法。我们可以通过 AttributeContainer 来创建属性字典,将修饰符函数链接到 AttributeContainer 来形成字典,而不是连续的命令式属性设置:

let att2 = AttributedString("Howdy",
    attributes: AttributeContainer()
        .font(UIFont(name:"Arial-BoldMT", size:15)!)
        .foregroundColor(UIColor(red:0.251, green:0.000, blue:0.502, alpha:1))
    )
print(att2)
Run Code Online (Sandbox Code Playgroud)

(在现实生活中我会说.init()而不是AttributeContainer()。)

所以我的问题是,这在语法上是如何工作的?我们这里似乎有一个 DSL,我们可以在其中根据属性键的名称链接看起来像函数调用的内容。在幕后,似乎有一些动态成员查找的组合,callAsFunction也许还有某种中间构建器对象。我可以看到每个callAsFunction调用都返回 AttributeContainer,这显然是链接的工作原理。但是我们如何编写自己的对象,使其在语法上的行为方式与 AttributeContainer 的行为方式相同呢?

Ale*_*ica 4

我过去制作过与此类似的 DSL。

我无法验证这是否正是他们正在做的事情,但我可以描述我实现类似 DSL 语法的方式。

我的构建器对象将具有类似.font.color返回临时@dynamicCallable struct. 这些结构将存储它们的父版本(以此类推, )AttributeContainer,以及它们被称为源自(\.font\.color等)的键路径。(我不记得我是否使用了正确的键路径或字符串。我可以稍后检查并回复您。)

的实现callAsFunction看起来像这样:

func callAsFunction(_ someParam: SomeType) -> AttributeContainer {
    parent[keyPath: keyPath] = someParam
    return parent // for further chaining in the fluent interface.
}
Run Code Online (Sandbox Code Playgroud)

随后的调用.foregroundColor将重复相同的过程。

这是一个简单的例子:

@dynamicMemberLookup struct DictBuilder<Value> {
    struct Helper<Value> {
        let key: String
        var parent: DictBuilder<Value>
        
        func callAsFunction(_ value: Value) -> DictBuilder<Value> {
            var copy = parent
            copy.dict[key] = value
            return copy
        }
    }
    
    var dict = [String: Value]()
    
    subscript(dynamicMember key: String) -> Helper<Value> {
        return DictBuilder.Helper(key: key, parent: self)
    }
}

let dict = DictBuilder<Int>()
    .a(1)
    .b(2)
    .c(3)
    .dict
    
print(dict)
Run Code Online (Sandbox Code Playgroud)

IIRC,您可以使用一些通用的魔术和键路径(而不是字符串)来为每个键路径返回不同的类型,这callAsFunciton可能需要不同类型的参数,这可以在编译时强制执行。

您可以使用@dynamicCallable而不是@dynamicMemberLookup+ callAsFunction,但我认为我刚才提到的技巧不起作用。