How do I override a func that is available before iOS 12.0

I'm trying to implement

override public func value(forUndefinedKey key: String) -> Any?


in an INIntent subclass, but when I do, I get this error message: "Overriding 'value' must be as available as declaration it overrides"


I believe this is due to the class declaration being wrapped in an @available(iOS 12.0, *) attribute. So, how do I override this method?



The reason I need to do this is that I've added a property to an existing Intent, and when I try to access this property ion an intent that was created before it was added, I get an NSUnknownKeyException. This is happening in iOS 12, and perhaps 13 (I don't recall). Should I create a new intent Class with this new property to avoid this, or is there a known workaround?

Replies

I cannot reproduce the exact message "Overriding 'value' must be as available as declaration it overrides", both in Xcode 10.3 and 11.

Please show the version of your Xcode, and enough code to reproduce the issue.


I do not believe it is due to the existence of `@available(iOS 12.0, *)`, but generally, overriding `value(forUndefinedKey:)` might not be the right solution to fix `NSUnknownKeyException`.


Please show your code on how you added your property and on how you access it.

Xcode 11 GM 2


@available(iOS 12.0, *) @available(iOSApplicationExtension 12.0, *)
extension MySiriIntent {
    override public func value(forUndefinedKey key: String) -> Any? {
        if key == "myKey" {
          //return an appropriate value for myKey
        }
        return nil
    }
}


(lldb) po [$arg1 name]

NSUnknownKeyException


(lldb) po [$arg1 reason]

'myKey' is an invalid parameter for 'MySiriIntent'. Please make sure that your intent definition file is valid.


Ok, looking closer, I see that I confused the NSUndefinedKeyException with NSUnknownKeyException, so, to your point, may be barking up the wrong tree here.


Does anyone know how iOS handles changes to siriIntent definitions? I've added a property, and now previous versions of the shortcut fail, because an NSUnkownKeyException i raised when trying to access the new property.

Some more details, this happens on a call to INGetIdProperty, which throws the NSUnknownKeyException. Looks like it is fixed in iOS 13, new properties are nil when an older versoin of the intent is loaded.