Monday, January 30, 2023
No menu items!
HomeiOS Developmentios - Is there a method to have delegate features in a...

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


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.measurement.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 positive, however the factor is I’ve a number of views with this similar actual code, besides that the extension is on every respective view. I believed perhaps there’s a method to scale 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.measurement.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 believe it has one thing to do with the features being delegate features that I do not name alone.

Anyway, is there a pleasant method to cope with this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments