无法分配类型为[[NSAttributedStringKey:Any]'的值来键入swift

Chi*_*dog 0 uitextview swift

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

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,
]
Run Code Online (Sandbox Code Playgroud)

但是它会出错:

无法将类型[[NSAttributedStringKey:任何]]的值分配给类型[[String:任何]?

我究竟做错了什么?

这是我的完整代码:

//
//  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,
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

Sh_*_*han 6

您可以尝试(Swift 4.2)

self.headerDescription.linkTextAttributes = [
    NSAttributedString.Key.foregroundColor: UIColor.blue,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
Run Code Online (Sandbox Code Playgroud)

迅捷4.0

self.headerDescription.linkTextAttributes = [
        NSAttributedStringKey.foregroundColor.rawValue  : UIColor.blue,
        NSAttributedStringKey.underlineStyle.rawValue  : NSUnderlineStyle.styleSingle.rawValue
]
Run Code Online (Sandbox Code Playgroud)