1

In Objective-C, I can do the following:

SomeClass<SomeProtocol> *foo = nil;

This tells the compiler that foo isa SomeClass, and conforms to the protocol SomeProtocol.

How can I do the same thing in Swift?

When I try to do the following:

@objc
protocol SomeProtocol {}

class SomeClass: NSObject {}

class ChildClass: SomeClass, SomeProtocol {}

class FooBar {
    init() {
        var foo: SomeClass<SomeProtocol>

    }
}

... it gives the error:

error: cannot specialize non-generic type 'SomeClass'
        var foo: SomeClass<SomeProtocol>
                 ^

This is because angel brackets (<>) are reserved for generics rather than protocols.

Senseful
  • 86,719
  • 67
  • 308
  • 465
  • Is this what you are looking for? – [How do I declare a variable that has a type and implements a protocol?](http://stackoverflow.com/questions/25214484/how-do-i-declare-a-variable-that-has-a-type-and-implements-a-protocol) – Martin R Mar 26 '15 at 18:27

0 Answers0