The dictionary now gives the error "cannot be converted to BooleanLiteralConvertible", since updating to Swift 1.2

I just dipped on Swift, and then Swift 1.2 appeared (breaking the working code)!

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

My previous working code:

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

Starting with Swift 1.2, the compiler provides two dictionary related errors options:

  • The type of expression is ambiguous without additional context.
  • '_' does not convert to 'BooleanLiteralConvertible' (in reference to true)

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

- ? , - , Swift 1.2 , .

+4
1

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
]

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

Source: https://habr.com/ru/post/1584705/


All Articles