关闭不需要返回任何内容的集合



我正在尝试编写一个函数来向NSAttributedString添加属性。我已经在字典中传递给了类似...

 tag      attributes
[String : [String : Any]]

然后,这些(标签和属性)将被传递到另一个函数中,以在给定标签中添加属性。

func addAttributes(attributes: [String: Any], forTag tag: String)

我能做的...

for (tag, attributes) in dictionary

但是有没有一种关闭方式呢?

如果我使用平面地图...

dictionary.flatMap { addAtritbutes(attributes: $1, insideTag: $0) }

然后它抱怨我没有返回任何内容/使用调用结果。有没有一个功能可以让我在没有警告的情况下执行此操作?

谢谢

您可以使用forEach闭包。

dictionary.forEach {
    addAtritbutes(attributes: $0.value, insideTag: $0.key)
}

相关内容

最新更新