AliSoftware/Reusable

Conformance of 'UICollectionViewCell' to protocol 'Reusable' was already stated in the type's module 'UIKit'

danmartyn opened this issue · 2 comments

I've added extensions for UITableViewCell, UITableViewHeaderFooterView, UICollectionViewCell and UICollectionReusableView in the protocol file:

extension UITableViewCell: Reusable {}
extension UITableViewHeaderFooterView: Reusable {}
extension UICollectionViewCell: Reusable {}
extension UICollectionReusableView: Reusable {}

So that I don't need to make all my custom cells/views conform to Reusable, and can assume all custom cells/views will be Reusable. This works, however, the compiler is complaining about the UICollectionViewCell with "Conformance of 'UICollectionViewCell' to protocol 'Reusable' was already stated in the type's module 'UIKit'".

I can't see how the collection view cell is trying to use Reusable in UIKit. As well, I tried changing the name of the protocol to ReusableView and it still complained. I also tried adding the name of my module before Reusable, so it became: extension UICollectionViewCell: MyModule.Reusable {}.

Any ideas how to make the compiler be quiet about this?

Thanks

Hi

This is indeed a very strange warning. Like you, I can't see how UICollectionViewCell could already conform to Reusable from inside UIKit… the warning is probably misleading and coming from another problem with the wrong diagnostics from the compiler, maybe?

Strangely enough, I opened a random project on my Mac which uses Reusable, and added extension UICollectionViewCell: Reusable {} somewhere in the code and rebuild the project, and:

  • I do have a new error after adding this new line, but which is justified, and is on an existing code I had in the project which looks like class LabelCell: UICollectionViewCell, NibReusable { … }, telling me Redundant conformance of 'LabelCell' to protocol 'Reusable'… but this one makes sense (since indeed now UICollectionViewCell is already Reusable and NibReusable is a typealias for NibLoadable & Reusable, so the compiler is right here)
  • But I don't reproduce the warning you have about UICollectionViewCell already conforming to Reusable in UIKit though.

What version of Xcode and Swift are you using? Maybe the difference is somewhere there?

Did you try to add the name of the Reusable module (and not you module) before Reusable (because the Reusable protocol comes from the Reusable framework/module, not your module)? Like extension UICollectionViewCell: Reusable.Reusable {}?


Also, side note and personal point of view: I also personally don't recommend making UICollectionViewCell directly conform to Reusable, because it has the potential risk of making collection view cells even not from your app conform to it, like cells used by other frameworks in your app, for example etc. So I prefer to mark only your own cell classes as conforming. I know it can be repetitive, but I prefer explicit conformance than global implicit conformance that could potentially "leak" to more parts of your app that you intend to. See also the notes in the README in this paragraph:

In case your custom UIViewController, UITableViewCell, etc… is intended to be subclassed and be the parent class of many classes in your app, it makes more sense to add the protocol conformance (StoryboardBased, Reusable, …) to the child classes (and mark them final) than adding the protocol on the parent, abstract class.

Using Xcode 9.2 and Swift 4.0. I hadn't thought about making all cells Reusable being a bad idea before though, and you raised a good point. I've gone back and removed those extensions, and instead made my base classes all conform to Reusable, since I know all my custom cells will be subclassing the base views anyways, and this fixes the warning. Thanks for the help!