Comments (4)
Hello
I see two potential reasons for the error you see
-
You pass
viewType: headerType
butheaderType
is a function returning your type, and you didn't call it. Didn't you want to useviewType: headerType()
instead, to call the function so that it returns the expected(UICollectionReusableView & Reusable).Type
? -
Sadly I'm not sure if the current version of Swift is capable of going to such levels of abstraction just yet. Tbh I'm actually quite surprised that
(UICollectionReusableView & Reusable).Type
would compile (meaning that Swift would somehow reference a metatype built dynamically from two protocols? reminds me of similar limitations of current version of Swift like not being able to make a meta type conform to a protocol, and other limitations like the ones forcing us to use type erasure, etc)
I think if you make your headerType()
function's return type be concrete type instead of (UICollectionReusableView & Reusable).Type
it would work. e.g. if you have class CommonRV: UICollectionReusableView, Reusable {}
then use class MyRV1: CommonRV
& class MyRV2: CommonRV
in your code, you could have func headerType() -> CommonRV.Type { /* return MyRV1.Type or MyRV2.Type depending on some condition */ }
and I think in that case it would work.
tl;dr I think what make Swift compiler unhappy here is that (UICollectionReusableView & Reusable).Type
is maybe a little too complex for it to grasp, being a meta-type of a combination of a class and a protocol, not sure it's able to handle that just yet.
If that test confirms my hypothesis nĀ°2, then your solution is either to do what I described above (make all your concrete classes of UICollectionReusableView that you want to use with Reusable be a subclass of class CommonRV: UICollectionReusableView, Reusable
and not subclasses of UICollectionReusableView
directly), or if you don't want to use inheritance, maybe we could find a workaround solution using type erasure? I'm not sure
from reusable.
The first potential reason you point is in fact a typo. In my example, the var and the func have the same name, but it's not the case in my code. I'll update my post to better reflect that.
About your second potential reason, I've tried to simplify like that:
typealias MyHeader = UICollectionReusableView & Reusable
//This func is responsible for returning the correct header class according to the given indexPath
func retrieveHeaderType() -> MyHeader.Type { ... }
But I've got the same issue as the typealias doesn't change anything from the compiler pov.
I've also thought about having all my headers inherit from MyHeader
but I wonder if it will correctly dequeue a MyHeader
or its subclass... I'll try.
from reusable.
if you have class CommonRV: UICollectionReusableView, Reusable {} then use class MyRV1: CommonRV & class MyRV2: CommonRV in your code, you could have func headerType() -> CommonRV.Type { /* return MyRV1.Type or MyRV2.Type depending on some condition */ } and I think in that case it would work.
It indeed compiles and works as expected :). Thanks!
from reusable.
Thanks for the follow up!
(And I hope a future version of Swift will allow such composed metatypes construct!)
from reusable.
Related Issues (20)
- getting error on using cellType(for indexPath:)
- Support for new API introduced on iOS 13
- MKMapView support
- Feature request - allow to use UITableViewCell with custom style
- Swift Package Manager: reusable deployment target warning HOT 3
- SPM xibs (Bundle.module) HOT 1
- Xcode 12.5 warnings HOT 2
- Swift tools version update too high, lower version build failed
- UITableViewHeaderFooterView - Warning with NibReusable
- Sure, go ahead!
- ## Summary
- Feature request - access to root xib view
- Support for reusable UITableViewHeaderFooterView subclasses?
- NibReusable should have bundle as an optional parameter HOT 5
- Swift 5 Support? HOT 2
- Undefined symbols when compiling for unit tests HOT 1
- Xcode 11 beta 4 compiler error HOT 1
- loadNibContent always loads nib named after base class HOT 14
- is there a way to override sceneStoryboard for StoryboardSceneBased HOT 2
- Swift Package Manager HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reusable.