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

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

概述我只是围绕 Swift – 然后来了 Swift 1.2(打破我的工作代码)! 我有一个基于NSHipster – CGImageSourceCreateThumbnailAtIndex的代码示例的函数. 我以前工作的代码是: import ImageIOfunc processImage(jpgImagePath: String, thumbSize: CGSize) { if l 我只是围绕 Swift – 然后来了 Swift 1.2(打破我的工作代码)!

我有一个基于@L_419_2@的代码示例的函数.

我以前工作的代码是:

import ImageIOfunc 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,options))                // do other stuff            }        }    }}

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

>表达式的类型不明确,没有更多的上下文
>’_’不能转换为’BooleanliteralConvertible'(在ref为’true’值时)

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

任何人都可以照亮我吗?
更重要的是,任何人都可以解释用Swift 1.2和字典改变的内容,这些都阻止了这种情况的发生.

解决方法 从Xcode 6.3发行说明:

The implicit conversions from brIDged Objective-C classes
(Nsstring/NSArray/NSDictionary) to their corresponding Swift value
types (String/Array/Dictionary) have been removed,making the Swift
type system simpler and more predictable.

您的案例中的问题是像KCGImageSourcethumbnailMaxPixelSize这样的CFStrings.这些不是自动的
转换为String了.两种可能的解决方

let options = [    kCGImageSourcethumbnailMaxPixelSize as String : maxSize,kCGImageSourceCreatethumbnailFromImageIfAbsent as String : true]

要么

let options : [Nsstring : AnyObject ] = [    kCGImageSourcethumbnailMaxPixelSize:  maxSize,kCGImageSourceCreatethumbnailFromImageIfAbsent: true]
总结

以上是内存溢出为你收集整理的自从更新到Swift 1.2以来,Dictionary现在给出错误’不能转换为BooleanLiteralConvertible’全部内容,希望文章能够帮你解决自从更新到Swift 1.2以来,Dictionary现在给出错误’不能转换为BooleanLiteralConvertible’所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存