“_”不可转换为 'String'
'_' is not convertible to 'String'
我正在尝试创建一个属性字典以传递给 NSString.drawInRect
。但是,Swift 不会让我这样做,因为以下代码会导致神秘的错误消息 '_' 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]
需要做哪些不同的事情?
font
是可选的。打开它以获得包裹在其中的字体。
(这个答案没有给马特的答案添加任何新内容,所以如果你赞成这个,记得也赞成他的)
font
是可选的,你可以用font!
解包,像这样:
let font = NSFontManager.sharedFontManager().fontWithFamily(...)
let color = NSColor.whiteColor()
let paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
let attributes = [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: font!,
NSForegroundColorAttributeName: color]
我正在尝试创建一个属性字典以传递给 NSString.drawInRect
。但是,Swift 不会让我这样做,因为以下代码会导致神秘的错误消息 '_' 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]
需要做哪些不同的事情?
font
是可选的。打开它以获得包裹在其中的字体。
(这个答案没有给马特的答案添加任何新内容,所以如果你赞成这个,记得也赞成他的)
font
是可选的,你可以用font!
解包,像这样:
let font = NSFontManager.sharedFontManager().fontWithFamily(...)
let color = NSColor.whiteColor()
let paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
let attributes = [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: font!,
NSForegroundColorAttributeName: color]