Klemen Zagar Klemen Zagar - 13 days ago 6
Swift Question

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

If I have an instance of

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

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

Answer

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

Comments