[英]In swift,“Type of expression is ambiguous without more context”?
let fontRef = CTFontCreateWithName("ArialMT", config.fontSize, nil);
[kCTFontAttributeName : fontRef];
[kCTFontAttributeName : fontRef]
gives me the following error: [kCTFontAttributeName : fontRef]
给我以下错误:
"Type of expression is ambiguous without more context"
“表达类型不明确,没有更多上下文”
You didn't defined type of your dictionary. 您没有定义字典的类型。 From your definition it is not clear what is dictionary type, this is ambiguity reason.
从您的定义尚不清楚什么是字典类型,这是模棱两可的原因。 You need to assign it to something to ensure that there is no type ambiguity.
您需要将其分配给某些对象,以确保没有类型歧义。 Also it is not a root error, as
kCTFontAttributeName
is a CFString
type that is not Hashable
, as result can't be a key of a dictionary, you need to convert it as as String
first. 这也不是根错误,因为
kCTFontAttributeName
是不可Hashable
的CFString
类型,因为结果不能是字典的键,您需要首先将其转换as String
。 The type of dictionary for NSAttributedString
should be [String: AnyObject]
So the proper answer will be let attributes: [String: AnyObject] = [kCTFontAttributeName as String : fontRef]
. NSAttributedString
的字典类型应该为[String: AnyObject]
因此正确的答案应该是let attributes: [String: AnyObject] = [kCTFontAttributeName as String : fontRef]
。
ps Also do not put ;
ps也不要放
;
at end of lines, it is not required for Swift anymore and pretty often counted as bad style. 在行尾,Swift不再需要它,并且通常被认为是不良样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.