繁体   English   中英

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

[英]Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]'

将我的代码从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))
        }
    }

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

您的属性字典将如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM