自从更新到Swift 1.2以来,Dictionary现在给出错误'不能转换为BooleanLiteralConvertible'

So *_* It 4 dictionary cgimage swift

我只是围绕Swift - 然后来了Swift 1.2(打破我的工作代码)!

我有一个基于NSHipster的代码示例的函数 - CGImageSourceCreateThumbnailAtIndex.

我以前工作的代码是:

import ImageIO

func processImage(jpgImagePath: String, thumbSize: CGSize) {

    if let path = NSBundle.mainBundle().pathForResource(jpgImagePath, ofType: "") {
        if let imageURL = NSURL(fileURLWithPath: path) {
            if let imageSource = CGImageSourceCreateWithURL(imageURL, nil) {

                let maxSize = max(thumbSize.width, thumbSize.height) / 2.0

                let options = [
                    kCGImageSourceThumbnailMaxPixelSize: maxSize,
                    kCGImageSourceCreateThumbnailFromImageIfAbsent: true
                ]

                let scaledImage = UIImage(CGImage: CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options))

                // do other stuff
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从Swift 1.2开始,编译器提供了两个与options字典相关的错误:

  1. 如果没有更多的上下文,表达的类型是不明确的
  2. '_'不能转换为'BooleanLiteralConvertible' (在ref为'true'值时)

我已经尝试了多种方法来明确声明选项字典中的类型(例如[String : Any],[CFString : Any].[Any : Any]).虽然这可以解决一个错误,但它们会引入其他错误.

任何人都可以照亮我吗?更重要的是,任何人都可以解释使用Swift 1.2和字典改变了什么,从而阻止了这项工作.

Mar*_*n R 7

从Xcode 6.3发行说明:

从桥接的Objective-C类(NSString/NSArray/NSDictionary)到其相应的Swift值类型(String/Array/Dictionary)的隐式转换已被删除,使Swift类型系统更简单,更可预测.

您的案例中的问题CFString就像是kCGImageSourceThumbnailMaxPixelSize.这些不会自动转换为String.两种可能的解决方

let options = [
    kCGImageSourceThumbnailMaxPixelSize as String : maxSize,
    kCGImageSourceCreateThumbnailFromImageIfAbsent as String : true
]
Run Code Online (Sandbox Code Playgroud)

要么

let options : [NSString : AnyObject ] = [
    kCGImageSourceThumbnailMaxPixelSize:  maxSize,
    kCGImageSourceCreateThumbnailFromImageIfAbsent: true
]
Run Code Online (Sandbox Code Playgroud)