【问题标题】:How to check two NSAttributedString.key of a NSAttributed string for equality?如何检查 NSAttributedstring 的两个 NSAttributedString.key 是否相等?
【发布时间】:2019-08-27 23:49:18
【问题描述】:

我正在实现一个设置游戏,其中我的卡片是由 NSAttributedString 作为属性标题组成的按钮。为了检查三个选定的卡片是否构成一组,我想比较每个属性字符串(它们是按钮上的属性标题)上的属性,因为每个属性都代表一个属性。这无法完成,因为我在比较它们时遇到了编译错误。

let at: [NSAttributedString.Key : Any] = [
            .strokeColor : UIColor.blue,
            .foregroundColor : UIColor.white,
            .strokeWidth : -7.0,
            .font : UIFont.systemFont(ofSize: 35)
        ]

let s = NSAttributedString(string: "●", attributes: at)
        let attribute: [NSAttributedString.Key : Any] = [
            .strokeColor : UIColor.blue,
            .foregroundColor : UIColor.white,
            .strokeWidth : -7.0,
            .font : UIFont.systemFont(ofSize: 35)
        ]
let a = NSAttributedString(string: "▲", attributes: attribute)

if(s.attributes(at: 0, effectiveRange: nil) & (a.attributes(at: 0, effectiveRange: nil)) // ## Compilation Error: Any doesn't conform to Eqautable Protocol ##

【问题讨论】:

  • 请在问题描述中添加您遇到的编译错误。谢谢

标签: swift4.2 nsattributedstringkey


【解决方案1】:

您可以使用NSAttributedString 实例func isEqual(to other: NSAttributedString) -> Bool 方法来实现这些目的。

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多