Doubt about the runtime system


(Ronaldo Faria Lima) #1

Hi, all.

I´m currently developing a solution that will apply a theme using appearance proxies. The idea is to have a configuration file where the developer will say which class he wants to customize and which properties with its corresponding values.

For instance, let´s say that one want to customize all navigation bar´s tint color property. So, the definitions for it will be put on a plist file as strings like:

UINavigationBar
    tintColor
        #442d5f

To do so, I tried to do something like this:

let aClass: AnyClass? = NSClassFromString(“UINavigationBar")
let proxy = (aClass as? UIAppearance.Type)?.appearance()
let sel = NSSelectorFromString("setTintColor:")
proxy?.perform(sel, with: UIColor.red)

However, doing that way is not secure at all. The /perform/ method may fail if the configured class does not have it. But, testing for the method existence by using /responds(to:)/ always return false.

It is worse if the class being customized is a specialization of some UIKit class. So, this is my question: is there a way to to load a class dynamically and inspect its properties/methods without knowing its type previously like I´m trying to do? I have tried to use a Mirror, but without success.

Thanks,

Ronaldo


(Brent Royal-Gordon) #2

That's odd. Could you use `aClass.instancesRespond(to: sel)` instead?

···

On Jun 6, 2017, at 6:55 AM, Ronaldo Faria Lima via swift-users <swift-users@swift.org> wrote:

The /perform/ method may fail if the configured class does not have it. But, testing for the method existence by using /responds(to:)/ always return false.

--
Brent Royal-Gordon
Architechies