gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:31:08 24 4
gpt4 key购买 nike

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

关于ios - 无法将类型 '[String : Any]' 的值转换为预期的参数类型 '[NSAttributedStringKey : Any]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55869454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com