Skip to content

Instantly share code, notes, and snippets.

@acrosa
Created December 5, 2014 23:14
Show Gist options
  • Select an option

  • Save acrosa/fd1e5aeeccafeb1d1a6b to your computer and use it in GitHub Desktop.

Select an option

Save acrosa/fd1e5aeeccafeb1d1a6b to your computer and use it in GitHub Desktop.
protocol ClassProtocol
{
var property: SomePropertyProtocol? { get set }
}
protocol SomePropertyProtocol
{
}
class SomeClass : ClassProtocol
{
var property: SomePropertyProtocol?
}
@drodriguez
Copy link

Best I can do:

protocol TestProtocol {
}

class ParentClass {
}

class TestClassA : ParentClass, TestProtocol {
}

class TestClassB : ParentClass, TestProtocol {
}

class TestClassC : ParentClass {
}

class TestClassD : TestProtocol {
}

struct Test<T where T: ParentClass, T: TestProtocol> {
  var a: T.Type {
    get {
      return T.self
    }
  }
}

let t1 = Test<TestClassA>()
let t2 = Test<TestClassB>()
let t3 = Test<TestClassC>() // Type 'TestClassC' does not conform to protocol 'TestProtocol'
let t4 = Test<TestClassD>() // Type 'TestClassD' does not inherit from 'ParentClass'

If you can, I will use a little protocol helper and call it a day:

protocol ParentClassProtocol {
}

class ParentClass: ParentClassProtocol {
}

struct Test2 {
  var b: protocol<ParentClassProtocol, TestProtocol>.Protocol {
    get {
      return TestClassA.self
    }
  }
}

You can add this protocol (a tag) in an extension for classes like UIView if you need.

The truth is that protocol<> should actually be type<> and work for real types...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment