Cannot convert value of type '[String : AnyObject]?' to expected argument type '[NSAttributedStringKey : Any]?'
This is a new feature of Swift 4. All the Cocoa methods that take string identifiers and/or dictionary keys now have their own types for the keys. The reason for this is to add a bit of type safety—in the old regime, it was possible to accidentally pass a String
constant by mistake that was meant to be used with some other API, but now in Swift 4, this will result in a compilation error.
Change your method signature to:
open class func drawText(context: CGContext, text: String, point: CGPoint,
align: NSTextAlignment, attributes: [NSAttributedString.Key : Any]?)
EDIT: Updated for Swift 4.2! NSAttributedStringKey
has been renamed to NSAttributedString.Key
.
Swift 4 Cannot convert value of type '[String : AnyObject]?' to expected argument type '[NSAttributedStringKey : Any]?'
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]
Cannot convert value of type 'NSAttributedString.Key' to expected dictionary key type 'String' error(swift4.2)
In Swift 4.2 you have to use NSAttributedString.Key
instead of String
let lineattribute : [NSAttributedString.Key : Any] = [
.foregroundColor : UIColor(hexString: "#0f88b7ff"),
.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]
let attributeString = NSMutableAttributedString(string: "View traveling details", attributes: lineattribute)
Swift 4 conversion error: Cannot convert value of type 'String' to expected argument type 'NSAttributedStringKey'
You need to remove the use of .rawValue
for the .paragraphStyle
key.
Also note that for Swift 4.2 (or later) you need to replace any use of NSAttributedStringKey
with NSAttributedString.Key
.
DBL_MAX
is deprecated. Use .greatestFiniteMagnitude
instead of CGFloat(DBL_MAX)
.
Force-unwrapping the optional lineBreakMode
parameter is going to cause your app to crash if nil
is passed in.
Cannot subscript a value of type '[NSAttributedStringKey : Any]' with an index of type 'String'
In Swift 4 - NSAttributedString representation is changed.
The types are not [String: AnyObject]
anymore. Its [NSAttributedStringKey:Any]
So your attributes should accessed like this:
if let attrs = attributes[NSAttributedStringKey.init("YYTextHighlight")] as? YYTextHighlight
Related Topics
Xcode Beta 6 "Type of Expression Is Ambiguous Without More Context" Navigationlink
Why How to Use Codable with a Project Language Version of Swift 3.3
Remove All Non-Numeric Characters from a String in Swift
How to Set Addobserver in Swiftui
Differencebetween Http Parameters and Http Headers
What's the Equivalent of Finally in Swift
How to Create Nsradiobutton Group in Xcode 7 Osx
Viewcontroller Slide Animation
In Swift, Can You Split a String by Another String, Not Just a Character
Mutable Binding in Swiftui Live Preview
Swift: How to Delete Part of Audio
Custom Swift Encoder/Decoder for the Strings Resource Format