gpt4 book ai didi

ios - NSAttributedStringKey 错误与 swift 4

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:33 25 4
gpt4 key购买 nike

更新到 Swift 4 后,我收到此代码的错误

    attributes["NSFont"] = font
attributes["NSColor"] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)

Cannot subscript a value of type '[NSAttributedStringKey : Any]' with an index of type 'String'

我能够通过将 ["NSFont"] 替换为 NSAttributedStringKey.font 来修复第一行,但我不确定如何修复第二行。

最佳答案

在 swift 4 中 - NSAttributedString 表示完全改变了。

替换您的属性字典 - attributes 类型,从 [String : Any][NSAttributedStringKey : Any][NSAttributedString .Key : Any],如果你还没有完成的话。

试试这个

swift 4.2+:

attributes[NSAttributedString.Key.font] = font
attributes[NSAttributedString.Key.foregroundColor] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)

swift 4.0 和 4.1:

attributes[NSAttributedStringKey.font] = font
attributes[NSAttributedStringKey.foregroundColor] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)

这是 Apple 文档中的注释:NSAttributedString.Key

关于ios - NSAttributedStringKey 错误与 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com