0

将我的代码从swift3 转换为 4 并得到错误 Cannot convert value of type '[String : Any]' to expected argument type '[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))
        }
    }
4

1 回答 1

3

Well, you initiated your boldAttributes variable with a [String : Any], and your error tells you this is not the expected type of variable. So initiating the variable with [NSAttributedString.Key : Any] and removing .rawValue should solve your problem.

Your attributes dictionary will be like this:

let boldAttributes: [NSAttributedString.Key : Any] = [
    .font: LocalConstants.termsBoldFont,
    .link: linkType.url
]
于 2019-04-26T14:16:19.913 回答