Klemen Zagar Klemen Zagar - 1 year ago 73
Swift Question

How can I add protocol to an element from interfacebuilder in Swift?

If I have an instance of

in storyboard and I have it connected to view controller as
. If I want add some functionality only to this
, how can I do it? I know that for obj-c you make a subclass of
. But I wonder if there is better approach in swift.

Is it possible for this specific
to conform to some protocol that I'll make.

Answer Source

You can declare a private extension in the same file you're using the UILabel

private extension UILabel {}

In this case only the components in the same file can use the new feature. I know, it's a kind of workaround, but in my cases where I create a single file per each component, it works.

Pay attention that the protocol conformance has to be public, so you can't use the private access level

EDIT: since you need a special configuration for only one UILabel in a single file, this way won't allow it. Subclassing is the only solution

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download