user2924482
user2924482

Reputation: 9120

Swift: Declaring a delegate in Struct

I want to declare a delegate in Swift Struct. For example in viewController you can load delegate:

class ViewController : UIViewController, UITableViewDelegate 

But in Struct file for example I try to the same :

struct sample:UITableViewDelegate

But I'm getting this error:

Non-Class type 'sample' cannot conform to class protocol NSObjectProtocol

How can I add a delegate to Struct file?

I'll really appreciate your help.

Upvotes: 1

Views: 3003

Answers (2)

Yury
Yury

Reputation: 6114

Structs in swift can only conform protocols that not marked as class protocols with keyword class, like CustomStringConvertible protocol

Upvotes: 2

Caio Tomazelli
Caio Tomazelli

Reputation: 513

The error says it all. You can't conform to an NSObjectProtocol (father class of all protocols) if you're not a class!

Consider using a vanilla class, if it has to conform to a protocol it should not be a struct at the first place!

Upvotes: 0

Related Questions