简体   繁体   中英

'_' is not convertible to 'String'

I'm trying to create a dictionary of attributes to pass into NSString.drawInRect . However, Swift won't let me, as the following code results in mysterious error message '_' is not convertible to 'String'

let font = NSFontManager.sharedFontManager().fontWithFamily(...)
let color = NSColor.whiteColor() 
let paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
let attributes = [
    NSParagraphStyleAttributeName: paragraphStyle,
    NSFontAttributeName: font,
    NSForegroundColorAttributeName: color]

What needs to be done differently?

(This answer add nothing new to matt's answer so if you upvote this, remember to upvote his as well)

font is optional, you can unwrap it with font! , like this:

let font = NSFontManager.sharedFontManager().fontWithFamily(...)
let color = NSColor.whiteColor() 
let paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
let attributes = [
    NSParagraphStyleAttributeName: paragraphStyle,
    NSFontAttributeName: font!,
    NSForegroundColorAttributeName: color]

font is an Optional. Unwrap it to get the font that's wrapped inside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM