ios – Is there a method to have delegate features in a protocol extension with conditional conformance?

[ad_1]

In a number of views I’ve an extension that implements the protocol UICollectionViewDelegateFlowLayout like this:

extension MyView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CGFloat((collectionView.body.dimension.width / 2) - 5), peak: CGFloat(165))
     }
      
     func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
         return 10
     }
      
     func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
         return 10
     }
}

It really works nice, however the factor is I’ve a number of views with this identical actual code, besides that the extension is on every respective view. I assumed possibly there’s a method to cut back the quantity of code utilizing a protocol extension that offers all entry to those features. I attempted one thing like this:

protocol A the place Self: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
      
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat
      
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat
}

extension A {
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CGFloat((collectionView.body.dimension.width / 2) - 5), peak: CGFloat(165))
     }
      
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
        return 10
    }
      
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
        return 10
    }
}

However this does not take, and I think it has one thing to do with the features being delegate features that I do not name by myself.

Anyway, is there a pleasant method to take care of this?

[ad_2]

Leave a Reply