【问题标题】:'_' is not convertible to 'String''_' 不能转换为 'String'
【发布时间】:2015-07-18 23:39:28
【问题描述】:

我正在尝试创建一个属性字典以传递给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]

需要做哪些不同的事情?

【问题讨论】:

    标签: macos swift cocoa swift2


    【解决方案1】:

    (此答案对马特的答案没有任何新意,所以如果您对此表示赞同,请记住也为他的回答投票)

    font 是可选的,你可以用font! 解开它,像这样:

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

    【讨论】:

    • Matt 拒绝在他的答案中提供示例,因此这将是公认的答案,因为它符合正确答案的 SO 标准。谢谢。
    【解决方案2】:

    font 是可选的。展开它以获取包含在其中的字体。

    【讨论】:

    • 如果您不同意我编辑的示例,请提供替代方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    相关资源
    最近更新 更多