Uploaded image for project: 'Swift'
  1. Swift
  2. SR-2388

Forming a CFDictionary from a Swift Dictionary is now really difficult

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode Version 8.0 beta 6 (8S201h)

      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. )

        Attachments

          Activity

            People

            • Assignee:
              jrose Jordan Rose
              Reporter:
              mattneub Matt Neuburg
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: