Alexander
Alexander

Reputation: 33

CFString doesn't conform to protocol Hashable?

I recently updated to Xcode 6.1 to be able to work with iOS 8.1, but now my latest project in facing an error.

I get the error saying that "CFString! does not conform to protocol Hashable", for the following line:

let attributes = [kCTForegroundColorAttributeName:UIColor.blackColor().CGColor, kCTFontAttributeName: font]

I did not get this error when running Xcode 6.0.1. And also, Xcode 6.1 is extremely slow. I mean, so slow that it doesn't actually finish loading anything. Indexing takes several minutes, and building takes so long that I haven't managed to sit through... It also crashes.

My main problem is with the Hashable protocol though. What's up with that?

Upvotes: 1

Views: 1104

Answers (1)

Darren
Darren

Reputation: 25619

This appears to be a type inference bug with Dictionaries that contain CF objects.

The compiler is (apparently) using the first key/value pair to infer a Dictionary of type [CFStringRef:CGColorRef], and then fails to compile because CFStringRef doesn't conform to Hashable.

You can work around the problem by explicitly declaring the Dictionary type:

let attributes : [String:AnyObject] = [
    kCTForegroundColorAttributeName:UIColor.blackColor().CGColor, 
    kCTFontAttributeName:font
]

Upvotes: 6

Related Questions