'NSHostingController subclass type including generics
This is a syntax problem. I am happily following along Daniel Steinberg's excellent SwiftUI Kickstart book, and a little while along, we have struct CheckerBoard which later becomes
struct CheckerBoard<A> where A: View
So far so good. If I use it in a SwiftUI-only app, as the book intended, @main instantiates an instance of Checkerboard and all is well.
If I want to use it in a storyboard app, I need to use an NSHostingController, and in order to do so, you need to specify a type.
Not a problem for
class SwiftUIHostingController: NSHostingController<CheckerBoard>
but a big problem when I try to amend this for struct CheckerBoard<A> where A: View
This is where I struggle (I've tried looking at a lot of SwiftUI tutorials, generics tutorials, and various descriptions of the problem, all without success. 'Generics' and 'where' are not good things to google for).
Fixit suggests
class SwiftUIHostingController: NSHostingController<CheckerBoard<<#A: View#>>> but that's as far as I get. I cannot work out what to replace A: View with.
NSHostingController<CheckerBoard<A where A: View>> fails with "Expected '>' to complete generic argument list", as does NSHostingController<CheckerBoard<A> where A: View> and NSHostingController<CheckerBoard<A: View>>.
Me wanting to use a View with a generic property appears to be rare enough that this has thrown the compiler completely: not only do I not get a useful hint for code completion in NSHostingController, but the PreviewProvider creates a rather panicked error of "Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project".
(This really should have the tag 'NSHostingController' but I cannot add it myself.)
Solution 1:[1]
You just need another level of specification, because only concrete controller can be placed into storyboard, something like
class TextCheckboardController: NSHostingController<CheckerBoard<Text>>
, for each specific A in corresponding place.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asperi |
