我对Xcode6.1GM有个奇怪的问题。
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)
错误在行中,让texFontAttributes...
Cannot convert the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible'
这段代码在Xcode6.1GM之前一直运行得很好。
当我尝试将textFontAttributes声明为NSDictionary时,错误消息更改为:
Cannot convert the expression's type 'NSDictionary' to type 'NSString!'
我不知道如何解决这个问题:
发布于 2014-10-05 21:35:08
问题是font
是可选的,因为方便的构造器现在返回可选值,所以font
需要被解包才能成为字典中的值:
if let actualFont = font {
let textFontAttributes = [
NSFontAttributeName: actualFont,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle
]
text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes)
}
发布于 2014-10-29 03:06:29
这也是另一种选择。
let textFontAttributes = [
NSFontAttributeName : font!,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle
]
发布于 2014-10-05 19:04:14
我的应用程序中有这段代码,它可以正常工作:
var textAttributes: [String: AnyObject] = [
NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 1.0).CGColor,
NSFontAttributeName : UIFont.systemFontOfSize(17)
]
https://stackoverflow.com/questions/26201844
复制相似问题