加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:35:14 所属栏目:百科 来源:网络整理
导读:我只是围绕 Swift – 然后来了 Swift 1.2(打破我的工作代码)! 我有一个基于NSHipster – CGImageSourceCreateThumbnailAtIndex的代码示例的函数. 我以前工作的代码是: import ImageIOfunc processImage(jpgImagePath: String,thumbSize: CGSize) { if let
我只是围绕 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,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
]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读