0

In Objective-c I would declare it like this

@protocol Protocol1
@end

@property (nonatomic, strong) UIViewController<Protocol1> property1;

How Can I declare property1 in Swift?

mohamede1945
  • 7,092
  • 6
  • 47
  • 61

1 Answers1

0

You might do something like that

class YourType: UIViewController, Protocol1 {}
var yourVar: YourType

Although it's a kind of hacky solution...

This could be even worse, still I am gonna add it

class YourClass<T:UIViewController, Protocol1> {
    var property1: T?
}
HepaKKes
  • 1,555
  • 13
  • 22
  • I want to expose this property in a library. This way, I tell all library customer's to use a specific class, which doesn't make sense. – mohamede1945 Apr 05 '15 at 17:34
  • Oh, I see, I think we should exploit the power of generics then. I am editing further my answer. But I am not sure it's gonna work as you intended – HepaKKes Apr 05 '15 at 17:39
  • Never mind, as I was further elaborating I realised that what I had in mind was even worse... :) I was thinking about exploiting generic types conforming to protocols. Check this out http://stackoverflow.com/questions/27633872/swift-generic-type-conforming-to-two-protocols. – HepaKKes Apr 05 '15 at 17:48
  • Thanks, but that's not what I want too. I've fixed it by having 2 properties (one for the UIViewController, another for the protocol) – mohamede1945 Apr 05 '15 at 18:16
  • 1
    I don't think there's a straightforward way to traslate that to Swift from Objective-C. Have you considered using a tuple, e.g. `var property1: (UITableViewController, Protocol1)?` instead of defining 2 separate properties? – HepaKKes Apr 05 '15 at 18:23