【问题标题】:Swift 4 Cannot convert value of type '[String : AnyObject]?' to expected argument type '[NSAttributedStringKey : Any]?'Swift 4 无法转换类型“[String:AnyObject]?”的值到预期的参数类型“[NSAttributedStringKey:Any]?”
【发布时间】:2018-03-01 13:42:18
【问题描述】:

我刚刚更新到 Xcode 9 并将我的应用程序从 swift 3 转换为 swift 4。 我有使用字符串来标记轴和其他变量的图表。 所以我有一个moneyAxisString = "Money"。 以前我可以使用以下代码绘制它们:

moneyAxisString.draw(in: CGRect(x: CGFloat(coordinateXOriginLT + axisLength/3), y: CGFloat(coordinateYOriginRT + axisLength + 5 * unitDim), width: CGFloat(300 * unitDim), height: CGFloat(100 * unitDim)), withAttributes: attributes as? [String : AnyObject])

其中attributes是定义如下的字典

 attributes = [
        NSAttributedStringKey.foregroundColor: fieldColor,
        NSAttributedStringKey.font: fieldFont!,
        NSAttributedStringKey.paragraphStyle: style

    ]

现在我的应用程序无法编译,我收到消息:

无法转换类型“[String : AnyObject]?”的值到预期的参数类型 '[NSAttributedStringKey : Any]?'

【问题讨论】:

    标签: swift4 xcode9


    【解决方案1】:

    这是类型不匹配:[String : AnyObject] 显然不是 [NSAttributedStringKey : Any]

    ⌥-单击 NSAttributedStringKey 以查看声明。


    解决方法是将attributes声明为

    var attributes = [NSAttributedStringKey : Any]()
    

    移除下投

     ..., withAttributes: attributes)
    

    简单的写

    attributes = [.foregroundColor: fieldColor,
                  .font: fieldFont!,
                  .paragraphStyle: style]
    

    【讨论】:

    • 当我删除向下转换时,我得到:无法将类型“NSDictionary”的值转换为预期的参数类型“[NSAttributedStringKey:Any]?”然后它说:插入' as! [NSAttributedStringKey:任何]'。此外,当我缩短属性的代码时,它说“表达式解析为未使用的左值”和“预期表达式”以及“一行上的连续语句必须用';'分隔。插入“;”。这是否相关我最初将属性定义为: var attributes: NSDictionary = [:]
    • 声明var attributes = [NSAttributedStringKey : Any]()而不是NSDictionary,然后你甚至可以删除注释。无论如何不要在 Swift 中使用 NSDictionary
    【解决方案2】:

    试试这个:

    class func getCustomStringStyle() -> [NSAttributedStringKey: Any]
        {
            return [
                NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 16), // or your fieldFont
                NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.black, // or your fieldColor
                NSAttributedStringKey(rawValue: NSAttributedStringKey.paragraphStyle.rawValue): NSParagraphStyle.default // or your style
            ]
        }
    

    或:

    class func getCustomStringStyle() -> [String: Any]
        {
            return [
                NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 16),
                NSAttributedStringKey.foregroundColor.rawValue: UIColor.black,
                NSAttributedStringKey.paragraphStyle.rawValue:NSParagraphStyle.default
            ]
        }
    

    【讨论】:

      【解决方案3】:

      NSAttributedStringKey 在 Swift 4 中更改为结构体。但是,使用 NSAttributedStringKey 的其他对象显然没有同时更新。

      最简单的解决方法,无需更改任何其他代码,就是追加 .rawValue所有你出现的@ 987654324@ setters - 将键名转换为Strings:

      let attributes = [
          NSAttributedStringKey.font.rawValue:  UIFont(name: "Helvetica-Bold", size: 15.0)!,
          NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
      ] as [String : Any]
      

      请注意,您现在也不需要as 上的!

      或者,您可以通过将数组声明为 [String : Any] 来跳过最后的 as 转换:

      let attributes: [String : Any] = [
          NSAttributedStringKey.font.rawValue:  UIFont(name: "Helvetica-Bold", size: 15.0)!,
          NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
      ]
      

      当然,您仍然需要为您设置的每个 NSAttributedStringKey 项目附加 .rawValue

      【讨论】:

        【解决方案4】:

        斯威夫特 4.2
        基于 user_Dennis 的示例构建

         func getCustomStringStyle() -> [NSAttributedString.Key: Any]
            {
                return [
                    NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 25), // or your fieldFont
                    NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.black, // or your fieldColor
                    NSAttributedString.Key(rawValue: NSAttributedString.Key.paragraphStyle.rawValue): NSParagraphStyle.default // or your style
                ]
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-28
          • 2019-09-16
          • 2015-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多