【问题标题】:Cannot assign value of type '[NSAttributedStringKey : Any]' to type swift无法分配类型“[NSAttributedStringKey:Any]”的值来快速键入
【发布时间】:2018-12-20 00:47:13
【问题描述】:

我正在尝试在我的应用中创建超链接,并且正在实施此解决方案:

let attributedString = NSMutableAttributedString(string: "Just click here to register")
let url = URL(string: "https://www.apple.com")!

// Set the 'click here' substring to be the link
attributedString.setAttributes([.link: url], range: NSMakeRange(5, 10))

self.headerDescription.attributedText = attributedString
self.headerDescription.isUserInteractionEnabled = true
self.headerDescription.isEditable = false

// Set how links should appear: blue and underlined
self.headerDescription.linkTextAttributes = [
    NSForegroundColorAttributeName: UIColor.blue,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
]

但它出错了:

无法将类型“[NSAttributedStringKey : Any]”的值分配给类型“[String : Any]?”

我做错了什么?

这是我的完整代码:

//
//  HeaderInfoTableViewCell.swift
//  Triage App
//
//  Created by Shay Vidas on 28/11/2018.
//  Copyright © 2018 Shay Vidas. All rights reserved.
//

import UIKit

class HeaderInfoTableViewCell: UITableViewCell {
    @IBOutlet weak var headerDescription: UITextView!

    override func awakeFromNib() {
        super.awakeFromNib()

        let attributedString = NSMutableAttributedString(string: "Just click here to register")
        let url = URL(string: "https://www.apple.com")!

        // Set the 'click here' substring to be the link
        attributedString.setAttributes([.link: url], range: NSMakeRange(5, 10))

        self.headerDescription.attributedText = attributedString
        self.headerDescription.isUserInteractionEnabled = true
        self.headerDescription.isEditable = false

        // Set how links should appear: blue and underlined
        self.headerDescription.linkTextAttributes = [
            NSForegroundColorAttributeName: UIColor.blue,
            NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
        ]
    }
}

【问题讨论】:

  • 根据您的 Swift 版本,linkTextAttributes 是一个字典,其键是 NSAttributedString.KeyString。在你的情况下,它们是String,所以你需要这样做:myStringKey.rawValue。但是在更新之间您使用了旧的NSForegroundColorAttributeName(类似于“Objective-C”),所以请保持一致。
  • 查看这个帖子:stackoverflow.com/questions/46314661/…,你可以使用headerDescription.linkTextAttributes = [ NSAttributedStringKey.foregroundColor.rawValue: UIColor.blue, NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue, ]

标签: swift uitextview


【解决方案1】:

你可以试试(Swift 4.2)

self.headerDescription.linkTextAttributes = [
    NSAttributedString.Key.foregroundColor: UIColor.blue,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]

斯威夫特 4.0

self.headerDescription.linkTextAttributes = [
        NSAttributedStringKey.foregroundColor.rawValue  : UIColor.blue,
        NSAttributedStringKey.underlineStyle.rawValue  : NSUnderlineStyle.styleSingle.rawValue
]

【讨论】:

  • 同样的问题:无法将类型“[NSAttributedStringKey : Any]”的值分配给类型“[String : Any]?”
  • 您使用哪个 swift 版本?
  • Sh_Khan 我正在使用 swift 4.0
  • 这很好,但是当我点击它时什么也没有发生......我的代码有问题吗?
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
相关资源
最近更新 更多