Michael Thomas
Michael Thomas

Reputation: 91

IBOutlet that conforms to a protocol?

In appears that in Swift you cannot create an IBOutlet that conforms to a protocol. My current work around is creating an IBOutlet of type AnyObject and a variable that conforms to my protocol and then setting said variable to the outlet in awakeFromNib() like so:

@IBOutlet private var fooOutlet: AnyObject!
private var foo: FooProtocol!

override func awakeFromNib() {
    super.awakeFromNib()

    foo = fooOutlet as? FooProtocol
}

My question is: Is there a better way to accomplish this ? or is this the only way that this can be accomplished in Swift?

Upvotes: 3

Views: 302

Answers (1)

user965972
user965972

Reputation: 2587

This will work

@objc(FooProtocol)
protocol FooProtocol {}

Upvotes: 3

Related Questions