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.