0

I'm stumped.

The title of this question the compiler error.

I am creating an object that basically marries the delegates of NSFetchedResultsController and UITableViewDataSource. The type parameter is used to indicate which NSManagedObject subclass should be used in the fetch requests.

Here is an example where Swift lacks dynamism and we end up writing all sorts of crazy code, (OR, I'm new to Swift and dislike being told what I'm not allowed to do)

So, on a UITableViewController subclass, i'd like to have a property

var dataSource: AnyDataSource<NSManagedObject>?

when I try to create one of these with a subclass of NSManagedObject and assign it to that property, the compiler complains. There seems to be nothing I can do that will succeed without a warning.

You would think that I should be able to use NSManagedObject or any of its subclasses, so I'm a little confused.

Any ideas? I'm using typical "type erasure" patterns that can be found on the internet, like via realm.io.

horseshoe7
  • 2,745
  • 2
  • 35
  • 49
  • But what if `AnyDataSource` has a method which takes an input of type `T`? If you could pass an `AnyDataSource` to a `AnyDataSource`, you could pass a `NSManagedObject` to a `NSManagedObjectSubclass`, which is illegal. – Hamish Jan 13 '17 at 17:28
  • Related: [How do I store a value of type Class in a Dictionary of type \[String:Class\] in Swift?](http://stackoverflow.com/q/38590548/2976878) – Hamish Jan 13 '17 at 17:30
  • T is constrained. where `T: NSManagedObject`, `T:NSFetchRequestResult` – horseshoe7 Jan 13 '17 at 17:36

1 Answers1

0

Ultimately I found that this approach was not possible. I mean, to achieve these with completely protocol-based programming.

I defined a few base protocols with no associated type, implemented an abstract baseclass that implements that protocol, then introduced generic type parameters in subclasses, that implement further protocols that have associated type.

I'll post my generalized solution on github when it's finished.

horseshoe7
  • 2,745
  • 2
  • 35
  • 49