【问题标题】:Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]'无法将类型“[String:Any]”的值转换为预期的参数类型“[NSAttributedStringKey:Any]”
【发布时间】:2019-09-16 01:50:23
【问题描述】:

将我的代码从 swift 3 转换为 4 并得到错误 无法将类型“[String : Any]”的值转换为预期的参数类型“[NSAttributedStringKey : Any]”

上线

attributedString.addAttributes(boldAttributes, range: NSRange(location: index, length: linkType.keyPhrase.count))

加粗属性突出显示

这是完整的代码

    private func addLink(_ linkType: AttributedURLType, attributedString: NSMutableAttributedString) {

        let indeces = attributedString.string.indices(of: linkType.keyPhrase)
        let boldAttributes: [String : Any] = [
            NSAttributedStringKey.font.rawValue: LocalConstants.termsBoldFont,
            NSAttributedStringKey.link.rawValue: linkType.url
        ]

        for index in indeces {
            attributedString.addAttributes(boldAttributes, range: NSRange(location: index, length: linkType.keyPhrase.count))
        }
    }

【问题讨论】:

标签: ios swift xcode facebook objectmapper


【解决方案1】:

好吧,您使用 [String : Any] 启动了您的 boldAttributes 变量,而您的错误告诉您这不是预期的变量类型。因此,使用 [NSAttributedString.Key : Any] 启动变量并删除 .rawValue 应该可以解决您的问题。

你的属性字典会是这样的:

let boldAttributes: [NSAttributedString.Key : Any] = [
    .font: LocalConstants.termsBoldFont,
    .link: linkType.url
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2016-07-27
    • 2016-07-02
    相关资源
    最近更新 更多