Swift 无法分配类型为';[NSAttributedStringKey:Any]';打字
我正在尝试在我的应用程序中创建超链接,我正在实施此解决方案:Swift 无法分配类型为';[NSAttributedStringKey:Any]';打字,swift,uitextview,Swift,Uitextview,我正在尝试在我的应用程序中创建超链接,我正在实施此解决方案: 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([.
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 4.2)
Swift 4.0
self.headerDescription.linkTextAttributes = [
NSAttributedStringKey.foregroundColor.rawValue : UIColor.blue,
NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle.rawValue
]
你可以试试(Swift 4.2)
Swift 4.0
self.headerDescription.linkTextAttributes = [
NSAttributedStringKey.foregroundColor.rawValue : UIColor.blue,
NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle.rawValue
]
根据您的Swift版本,
linktexttributes
是一个字典,其中键是NSAttributedString.Key
或String
。在您的例子中,它们是String
,因此您需要执行:myStringKey.rawValue
。但是在更新之间,您使用的是旧的NSForegroundColorAttributeName
(看起来像“Objective-C”),因此请保持连贯。检查此帖子:,您可以使用headerDescription.linkTextAttributes=[NSAttributedStringKey.foregroundColor.rawValue:UIColor.blue,NSAttributedStringKey.underlineStyle.rawValue:NSUnderlineStyle.styleSingle.rawValue,]
根据您的Swift版本,linktexttributes
是一个字典,其中键是NSAttributedString.Key
或String
。在您的情况下,它们是String
,因此您需要执行以下操作:myStringKey.rawValue
。但在更新之间,您使用了旧的NSForegroundColorAttributeName
(看起来像“Objective-C”),所以请连贯一致。检查此帖子:,您可以使用headerDescription.linkTextAttributes=[NSAttributedStringKey.foregroundColor.rawValue:UIColor.blue,NSAttributedStringKey.underlineStyle.rawValue:NSUnderlineStyleSingle.rawValue,]
同样的问题:无法将“[NSAttributedStringKey:Any]”类型的值分配给“[String:Any]”类型?您使用的是哪种swift版本?Sh_Khan我使用的是swift 4.0它很棒,但当我单击它时,什么都没有发生……我的代码有问题吗?同样的问题:无法将“[NSAttributedStringKey:Any]”类型的值分配给类型[String:Any]?“您使用的是哪种swift版本?Shu Khan我使用的是swift 4.0它很棒,但当我单击它时,什么都没有发生……我的代码有问题吗?”?