Swift 4 Unable to convert value of type '[String: AnyObject]?' to the expected argument type '[NSAttributedStringKey: Any]?'

I just upgraded to Xcode 9 and converted my application from fast 3 to fast 4. I have graphs that use strings to indicate axes and other variables. Therefore, I have moneyAxisString = "Money." I used to be able to draw them using this code:

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])

If the attributes are dictionaries defined as follows

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

    ]

Now my application will not compile, and I get a message:

Cannot convert value of type '[String: AnyObject]?' to the expected argument type '[NSAttributedStringKey: Any]?'

+5
source share
4 answers

: [String : AnyObject] [NSAttributedStringKey : Any]

⌥-click NSAttributedStringKey, .


attributes

var attributes = [NSAttributedStringKey : Any]()

 ..., withAttributes: attributes)

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

:

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
        ]
    }
+2

NSAttributedStringKey Swift 4. , NSAttributedStringKey, -, .

- - .rawValue NSAttributedStringKey - String s:

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

, ! as.

as , [String : Any] upfront:

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

, .rawValue NSAttributedStringKey, .

+2
source

Swift 4.2
Built using user_Dennis as an example

 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
        ]
    }
0
source

Source: https://habr.com/ru/post/1686042/


All Articles