无法将类型“ [String:Any]”的值转换为预期的参数类型“ [NSAttributedStringKey:Any]”

时间:2019-04-26 14:11:55

标签: ios swift xcode facebook objectmapper

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

在线

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))
        }
    }

1 个答案:

答案 0 :(得分:3)

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

您的属性字典如下:

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