[英]Cannot convert value of type '[String : Any]' to expected argument type 'String'
[英]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.