Hai . Z
Hai . Z

Reputation: 121

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:

"Type of expression is ambiguous without more context"

Upvotes: 0

Views: 2501

Answers (1)

Nikita Leonov
Nikita Leonov

Reputation: 5694

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. The type of dictionary for NSAttributedString should be [String: AnyObject] So the proper answer will be let attributes: [String: AnyObject] = [kCTFontAttributeName as String : fontRef].

ps Also do not put ; at end of lines, it is not required for Swift anymore and pretty often counted as bad style.

Upvotes: 1

Related Questions