Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift4

xcode9

I have just updated to Xcode 9 and converted my app from swift 3 to swift 4. I have graphs which use strings to label the axes and other variables. So I have a moneyAxisString = "Money". Previously I was 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])

Where attributes is a dictionary defined as follows

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

    ]

Now my app won't compile and I am getting the message:

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

like image 469
Steblo Avatar asked Sep 20 '17 13:09

Steblo


3 Answers

It's a type mismatch: [String : AnyObject] is clearly not [NSAttributedStringKey : Any]

⌥-click on NSAttributedStringKey to see the declaration.


The solution is to declare attributes as

var attributes = [NSAttributedStringKey : Any]()

to remove the down cast

 ..., withAttributes: attributes)

and to write simply

attributes = [.foregroundColor: fieldColor,
              .font: fieldFont!,
              .paragraphStyle: style]
like image 52
vadian Avatar answered Nov 19 '22 12:11

vadian


NSAttributedStringKey was changed to a struct in Swift 4. However, other objects that use NSAttributedStringKey apparently didn't get updated at the same time.

The easiest fix, without having to change any of your other code, is to append .rawValue to all your occurrences of NSAttributedStringKey setters - turning the key names into Strings:

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

Note that you won't need the ! at the as now, either.

Alternatively, you can skip the as cast at the end by declaring the array to be [String : Any] upfront:

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

Of course, you still need to append the .rawValue for each NSAttributedStringKey item you set.

like image 20
leanne Avatar answered Nov 19 '22 14:11

leanne


Try this:

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

or:

class func getCustomStringStyle() -> [String: Any]
    {
        return [
            NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 16),
            NSAttributedStringKey.foregroundColor.rawValue: UIColor.black,
            NSAttributedStringKey.paragraphStyle.rawValue:NSParagraphStyle.default
        ]
    }
like image 2
user_Dennis_Mostajo Avatar answered Nov 19 '22 14:11

user_Dennis_Mostajo