本文介绍了自从更新为Swift 1.2以来,字典现在给出错误'不可转换为BooleanLiteralConvertible'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个基于代码示例的函数, 。



我以前的工作代码是:

  import ImageIO 

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

如果let path = NSBundle.mainBundle()。pathForResource(jpgImagePath,ofType:){
如果让imageURL = NSURL(fileURLWithPath:path){
如果让imageSource = CGImageSourceCreateWithURL(imageURL,nil){

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

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

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

//做其他的东西
}
}
}
}

由于Swift 1.2,编译器提供了与选项相关的两个错误字典:


  1. 没有更多上下文
  2. b
  3. '_'不能转换为'BooleanLiteralConvertible'(在ref中为'true'值)

我已经尝试了各种方式来专门声明选项字典中的类型(例如。 [String:Any] [CFString:Any] [Any:Any] / code>)。虽然这可能会解决一个错误,但它们会引入其他错误。



任何人都可以照亮我?
更重要的是,任何人都可以解释Swift 1.2发生的变化以及停止工作的字典。

解决方案

从Xcode 6.3发行说明:

您的案例中的问题是 CFString kCGImageSourceThumbnailMaxPixelSize 。这些不会自动
转换为 String 。两个可能的解决方案:

  let options = [
kCGImageSourceThumbnailMaxPixelSize as String:maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent as String:真
]

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


I'm was just getting my head around Swift - then came Swift 1.2 (breaking my working code)!

I have a function based on the code sample from NSHipster - CGImageSourceCreateThumbnailAtIndex.

My previously working code is:

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
            }
        }
    }
}

Since Swift 1.2, the compiler provides two errors relating to the options dictionary:

  1. Type of expression is ambiguous without more context
  2. '_' is not convertible to 'BooleanLiteralConvertible' (in ref to 'true' value)

I have tried a variety ways to specifically declare the types in the options dictionary (eg. [String : Any], [CFString : Any], [Any : Any] ). While this may solve one error, they introduce other errors.

Can anyone please illuminate me??More importantly, can anyone please explain what changed with Swift 1.2 and dictionaries that stopped this from working.

解决方案

From the Xcode 6.3 release notes:

The problem in your case are the CFStrings like kCGImageSourceThumbnailMaxPixelSize. These are not automaticallyconverted to String anymore. Two possible solutions:

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

or

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

这篇关于自从更新为Swift 1.2以来,字典现在给出错误'不可转换为BooleanLiteralConvertible'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:04