ios - 无法使用类型为 '[NSAttributedStringKey : Any]' 的索引下标类型为 'String' 的值

标签 ios swift swift4 nsattributedstring

<分区>

我正在用 Swift 4.0 更新我的源代码我面临以下与属性文本相关的问题

以下代码在 swift 3.2 中运行良好。

entemessageText.highlightTapAction = { (containerView: UIView, text: NSAttributedString, range: NSRange, rect: CGRect) -> () in
        let attributes = text.attributes(at: range.location, effectiveRange: nil)
        if let attrs = attributes["YYTextHighlight"] as? YYTextHighlight {
            if let url = attrs.userInfo!["url"] as? String {
                self.openUrl(URL(string: url)!)
            }
        }
    }

“无法使用类型为‘String’的索引下标类型为‘[NSAttributedStringKey:Any]’的值”enter image description here

我用相同的关键字搜索解决方案,但没有得到任何结果。

任何帮助或建议都会有所帮助。

最佳答案

在 Swift 4 中 - NSAttributedString 表示已更改。

类型不再是[String: AnyObject]。它的 [NSAttributedStringKey:Any]

所以你的属性应该像这样访问:

if let attrs = attributes[NSAttributedStringKey.init("YYTextHighlight")] as? YYTextHighlight

关于ios - 无法使用类型为 '[NSAttributedStringKey : Any]' 的索引下标类型为 'String' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48297878/

相关文章:

ios - 如何在 Swift 4 中通过 POST 请求发送图像?

ios - 如何在 iOS 上通过地址找出导致崩溃的数组的名称?

ios - 当我将 Charts UIView 作为 IBOutlet 添加到 ViewController 时,应用程序崩溃

ios - 隐藏单元格中的自定义 UITableviewcell 更新

swift - 如何在每次再次加载 View 时清除 tableView 数据和 reloadData

ios - 如何使用 Google API 从互联网获取图像并将第一个结果插入 UIImageView 中

ios - 如何在Swift 3中解析JSON数组

Swift addConstraint 导致 EXC_BAD_INSTRUCTION

ios - swift : Create a multi-function multicast delegate

swift - `Calendar` API 崩溃 Swift Playground