Skip to content

[SR-2388] Forming a CFDictionary from a Swift Dictionary is now really difficult #44995

@mattneub

Description

@mattneub
Previous ID SR-2388
Radar rdar://problem/32003073
Original Reporter @mattneub
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode Version 8.0 beta 6 (8S201h)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @belkadan
Priority Medium

md5: 0803c238279e54d409d7b137b9e8c7d0

Issue Description:

Before seed 6, I used to be able to say this:

        let d : [NSObject:AnyObject] = [
            kCGImageSourceShouldAllowFloat : true,
            kCGImageSourceCreateThumbnailWithTransform : true,
            kCGImageSourceCreateThumbnailFromImageAlways : true,
            kCGImageSourceThumbnailMaxPixelSize : w
        ]
        let imref = CGImageSourceCreateThumbnailAtIndex(src, 0, d)!

Now I have to take everything across the bridge by hand. All the CFString keys have to be cast as String or NSString. We don't get any automatic bridge-crossing, so we have to tell Swift explicitly what the value types are, and then we have to cast the resulting dictionary to CFDictionary as well. Even if we get past the compiler, it is still possible to do this wrong and thus end up with an invalid CFDictionary so that at runtime CGImageSourceCreateThumbnailAtIndex yields nil. The only way I've found to get things right is like this:

        let d = [
            kCGImageSourceShouldAllowFloat as String : true as NSNumber,
            kCGImageSourceCreateThumbnailWithTransform as String : true as NSNumber,
            kCGImageSourceCreateThumbnailFromImageAlways as String : true as NSNumber,
            kCGImageSourceThumbnailMaxPixelSize as String : w as NSNumber
        ]
        let imref = CGImageSourceCreateThumbnailAtIndex(src, 0, d as CFDictionary)!

This is absolutely horrible.

The situation is compounded by the fact that no one seems to have told NSDictionary that its Swift equivalent is now [AnyHashable : Any]. We see that in the APIs and it works, but when you try to cast a CFDictionary to it, the compiler complains:

        let result = CGImageSourceCopyPropertiesAtIndex(src, 0, nil)! as [AnyHashable:Any]
        // compile error

In my opinion, Swift now goes too far with its refusal to help things get across the bridges, the Swift to Objective-C bridge and the CFTypeRef toll-free bridge. (In fact, there now seems to be rather a heavy toll on that bridge. 🙂)

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions