Overriding properties with limited availability

Hi everyone,

Something I’ve come across in iOS 11 is that you can’t override properties that are only available on iOS 11 when deploying with a deployment target less than the current target.

For example the following code is invalid:

extension UIViewController {
    
    @available(iOS, introduced: 11.0)
    open override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Do work here only relevant to iOS 11
        }
    }
}

This would be easily overridden in Obj-C, but I can’t do it in Swift. Is there any reason why this limitation exists?

Thanks,

Rod

Looks like this is a duplicate of this issue:

I’m curious if anyone knows whether that is actually a bug, or a behavioural choice on Swift’s part?

···

On 2 Aug 2017, at 10:31 am, Rod Brown via swift-users <swift-users@swift.org> wrote:

Hi everyone,

Something I’ve come across in iOS 11 is that you can’t override properties that are only available on iOS 11 when deploying with a deployment target less than the current target.

For example the following code is invalid:

extension UIViewController {
    
    @available(iOS, introduced: 11.0)
    open override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Do work here only relevant to iOS 11
        }
    }
}

This would be easily overridden in Obj-C, but I can’t do it in Swift. Is there any reason why this limitation exists?

Thanks,

Rod
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I'm not sure why you're using 'override' in an extension. Did you mean to put that in a subclass? It seems to work for me there.

Yes, Swift will not let you replace existing methods/properties using an extension if it can statically see that the method/property already exists. (It's not even safe to do that in Objective-C, but the language doesn't stop you.)

Jordan

···

On Aug 1, 2017, at 22:46, Rod Brown via swift-users <swift-users@swift.org> wrote:

Looks like this is a duplicate of this issue:

[SR-1486] @available with override causing <unknown>:0: error · Issue #44095 · apple/swift · GitHub

I’m curious if anyone knows whether that is actually a bug, or a behavioural choice on Swift’s part?

On 2 Aug 2017, at 10:31 am, Rod Brown via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi everyone,

Something I’ve come across in iOS 11 is that you can’t override properties that are only available on iOS 11 when deploying with a deployment target less than the current target.

For example the following code is invalid:

extension UIViewController {
    
    @available(iOS, introduced: 11.0)
    open override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Do work here only relevant to iOS 11
        }
    }
}

This would be easily overridden in Obj-C, but I can’t do it in Swift. Is there any reason why this limitation exists?

Thanks,

Rod
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Jordan,

So sorry, my snippet really wasn’t helpful there! Gah. I realise the extension wasn’t right! Sorry *facepalm*.

So I have a project. Its deployment target is iOS 10 (because we need 10.0 compatibility for the foreseeable future).

If I add the following code I get errors when it builds:

class MyViewControllerSubclass: UIViewController {
    
    @available(iOS, introduced: 11.0)
    override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Need to do work here
        }
    }
    
}

My only solution at this stage is currently to KVO on the AdditionalSafeAreaInsets property, which works, but this seems to point out a language deficiency.

Thanks

···

On 3 Aug 2017, at 3:24 am, Jordan Rose <jordan_rose@apple.com> wrote:

I'm not sure why you're using 'override' in an extension. Did you mean to put that in a subclass? It seems to work for me there.

Yes, Swift will not let you replace existing methods/properties using an extension if it can statically see that the method/property already exists. (It's not even safe to do that in Objective-C, but the language doesn't stop you.)

Jordan

On Aug 1, 2017, at 22:46, Rod Brown via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Looks like this is a duplicate of this issue:

[SR-1486] @available with override causing <unknown>:0: error · Issue #44095 · apple/swift · GitHub

I’m curious if anyone knows whether that is actually a bug, or a behavioural choice on Swift’s part?

On 2 Aug 2017, at 10:31 am, Rod Brown via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi everyone,

Something I’ve come across in iOS 11 is that you can’t override properties that are only available on iOS 11 when deploying with a deployment target less than the current target.

For example the following code is invalid:

extension UIViewController {
    
    @available(iOS, introduced: 11.0)
    open override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Do work here only relevant to iOS 11
        }
    }
}

This would be easily overridden in Obj-C, but I can’t do it in Swift. Is there any reason why this limitation exists?

Thanks,

Rod
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Oops, I didn't notice that my own deployment target was already set to iOS 11. I see what you're seeing now. Can you file a bug for this at https://bugs.swift.org <Issues · apple/swift · GitHub; ?

Workaround: put the override in an extension, and put the availability on the extension declaration. That seems to work for me.

Sorry for the trouble!
Jordan

···

On Aug 2, 2017, at 14:12, Rod Brown <rodney.brown6@icloud.com> wrote:

Jordan,

So sorry, my snippet really wasn’t helpful there! Gah. I realise the extension wasn’t right! Sorry *facepalm*.

So I have a project. Its deployment target is iOS 10 (because we need 10.0 compatibility for the foreseeable future).

If I add the following code I get errors when it builds:

class MyViewControllerSubclass: UIViewController {
    
    @available(iOS, introduced: 11.0)
    override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Need to do work here
        }
    }
    
}

<Screen Shot 2017-08-03 at 7.08.24 am.png>

My only solution at this stage is currently to KVO on the AdditionalSafeAreaInsets property, which works, but this seems to point out a language deficiency.

Thanks

On 3 Aug 2017, at 3:24 am, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

I'm not sure why you're using 'override' in an extension. Did you mean to put that in a subclass? It seems to work for me there.

Yes, Swift will not let you replace existing methods/properties using an extension if it can statically see that the method/property already exists. (It's not even safe to do that in Objective-C, but the language doesn't stop you.)

Jordan

On Aug 1, 2017, at 22:46, Rod Brown via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Looks like this is a duplicate of this issue:

[SR-1486] @available with override causing <unknown>:0: error · Issue #44095 · apple/swift · GitHub

I’m curious if anyone knows whether that is actually a bug, or a behavioural choice on Swift’s part?

On 2 Aug 2017, at 10:31 am, Rod Brown via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi everyone,

Something I’ve come across in iOS 11 is that you can’t override properties that are only available on iOS 11 when deploying with a deployment target less than the current target.

For example the following code is invalid:

extension UIViewController {
    
    @available(iOS, introduced: 11.0)
    open override var additionalSafeAreaInsets: UIEdgeInsets {
        didSet {
            // Do work here only relevant to iOS 11
        }
    }
}

This would be easily overridden in Obj-C, but I can’t do it in Swift. Is there any reason why this limitation exists?

Thanks,

Rod
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users