Swift 3 Questions


(simon gladman) #1

Hi All,

I thought I'd attempt updating the code of my Filterpedia
<https://github.com/FlexMonkey/Filterpedia> app to Swift 3. The transition
has been pretty smooth so far, but I have a few issues. I guess my question
over these issues is, are they features or bugs in an early Swift 3 release?

First off, overriding CIFilter requires overriding the *attributes* var
which is of type *[String: AnyObject*]. In Swift 2, the following works
perfectly:

    override var attributes: [String : AnyObject] {

        return [

                   kCIAttributeFilterDisplayName: "CMYK Levels",

                   "inputImage": [kCIAttributeIdentity: 0,

                                  kCIAttributeClass: "CIImage",

                                  kCIAttributeDisplayName: "Image",

                                  kCIAttributeType: kCIAttributeTypeImage]

        ]

    }

However, because the inputImage dictionary entry is a dictionary itself and
kCIAttributeTypeImage is a string, in Swift 3, it appears I have to
explicitly case them to compile:

    override var attributes: [String : AnyObject] {

        return [

                   kCIAttributeFilterDisplayName: "CMYK Levels",

                   "inputImage": [kCIAttributeIdentity: 0,

                                  kCIAttributeClass: "CIImage",

                                  kCIAttributeDisplayName: "Image",

                                  kCIAttributeType: kCIAttributeTypeImage as
AnyObject] as AnyObject

        ]

    }

There's a similar casting issue with the *arguments* parameter of a
kernel's *apply* method. In Swift 2, the following compiles without issue:

        let kernel = CIColorKernel()

        let rect = CGRect(

            x: 0, y: 0,

            width: 100, height: 100)

        let inputRadius: CGFloat = 5.678

        let inputCenter = CIVector(x: 12.3, y: 45.6)

        let inputRect = CIVector(cgRect: rect)

        let inputString = "Xyzzy"

        let arguments = [inputRect,

                         inputRadius,

                         inputCenter,

                         inputString]

        kernel.apply(

            withExtent: rect,

            arguments: arguments)

While in 3, I need to do the following:

        let arguments = [inputRect,

                         inputRadius as AnyObject,

                         inputCenter,

                         inputString as AnyObject] as [AnyObject]

Any thoughts anybody?

Cheers!

Simon

Simon Gladman +44 7973 669691

Blog: http://flexmonkey.blogspot.co.uk
GitHub: https://github.com/FlexMonkey
Twitter: @FlexMonkey <https://twitter.com/FlexMonkey>


(Austin Zheng) #2

Hi Simon,

This behavior is intentional; implicit bridging conversions are slated for removal in Swift 3.

More information: https://github.com/apple/swift-evolution/blob/master/proposals/0072-eliminate-implicit-bridging-conversions.md

Best,
Austin

···

On May 23, 2016, at 12:44 AM, simon gladman via swift-users <swift-users@swift.org> wrote:

Hi All,

I thought I'd attempt updating the code of my Filterpedia <https://github.com/FlexMonkey/Filterpedia> app to Swift 3. The transition has been pretty smooth so far, but I have a few issues. I guess my question over these issues is, are they features or bugs in an early Swift 3 release?

First off, overriding CIFilter requires overriding the attributes var which is of type [String: AnyObject]. In Swift 2, the following works perfectly:

    override var attributes: [String : AnyObject] {
        return [
                   kCIAttributeFilterDisplayName: "CMYK Levels",
                   "inputImage": [kCIAttributeIdentity: 0,
                                  kCIAttributeClass: "CIImage",
                                  kCIAttributeDisplayName: "Image",
                                  kCIAttributeType: kCIAttributeTypeImage]
        ]
    }

However, because the inputImage dictionary entry is a dictionary itself and kCIAttributeTypeImage is a string, in Swift 3, it appears I have to explicitly case them to compile:

    override var attributes: [String : AnyObject] {
        return [
                   kCIAttributeFilterDisplayName: "CMYK Levels",
                   "inputImage": [kCIAttributeIdentity: 0,
                                  kCIAttributeClass: "CIImage",
                                  kCIAttributeDisplayName: "Image",
                                  kCIAttributeType: kCIAttributeTypeImage as AnyObject] as AnyObject
        ]
    }

There's a similar casting issue with the arguments parameter of a kernel's apply method. In Swift 2, the following compiles without issue:

        let kernel = CIColorKernel()
        
        let rect = CGRect(
            x: 0, y: 0,
            width: 100, height: 100)
        
        let inputRadius: CGFloat = 5.678
        let inputCenter = CIVector(x: 12.3, y: 45.6)
        let inputRect = CIVector(cgRect: rect)
        let inputString = "Xyzzy"
        
        let arguments = [inputRect,
                         inputRadius,
                         inputCenter,
                         inputString]
        
        kernel.apply(
            withExtent: rect,
            arguments: arguments)

While in 3, I need to do the following:

        let arguments = [inputRect,
                         inputRadius as AnyObject,
                         inputCenter,
                         inputString as AnyObject] as [AnyObject]

Any thoughts anybody?

Cheers!

Simon

Simon Gladman +44 7973 669691 <tel:%2B44%207973%20669691>

Blog: http://flexmonkey.blogspot.co.uk <http://flexmonkey.blogspot.co.uk/>
GitHub: https://github.com/FlexMonkey
Twitter: @FlexMonkey <https://twitter.com/FlexMonkey>
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users