【发布时间】:2014-10-05 10:44:25
【问题描述】:
我对 Xcode 6.1 GM 有一个奇怪的问题。
let text: NSString = "A"
let font = NSFont(name: "Helvetica Bold", size: 14.0)
let textRect: NSRect = NSMakeRect(5, 3, 125, 18)
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.LeftTextAlignment
let textColor = NSColor(calibratedRed: 0.147, green: 0.222, blue: 0.162, alpha: 1.0)
let textFontAttributes = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle
]
text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes)
Error is in line let texFontAttributes...
Cannot convert the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible'
此代码在 Xcode 6.1 GM 之前可以完美运行。
当我尝试将 textFontAttributes 声明为 NSDictionary 时,错误消息更改为:
Cannot convert the expression's type 'NSDictionary' to type 'NSString!'
我不知道如何解决这个问题:(
【问题讨论】:
-
我不知道为什么,但
drawAtPoint:withAttributes:、drawInRect:withAttributes:、drawWithRect:options:attributes:、sizeWithAttributes:和boundingRectWithSize:options:attributes:是“在 Swift 中不可用” -
@JDS 它在 Swift 中不可用,但在 Swift 的 String 类型中不可用。您可以像 OP 一样在 NSString 上调用这些方法。
标签: swift