Created
November 12, 2015 16:41
-
-
Save bgerstle/a9c060fc31de9c3bed08 to your computer and use it in GitHub Desktop.
Demonstrating protocol extensions on UIViewController.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public protocol CellContainer { | |
var visibleCells: [NSIndexPath] { get } | |
} | |
public protocol CollectionViewContainer { | |
var collectionView: UICollectionView? { get } | |
} | |
extension CellContainer where Self: CollectionViewContainer { | |
public var visibleCells: [NSIndexPath] { | |
get { | |
return self.collectionView?.indexPathsForVisibleItems() ?? [] | |
} | |
} | |
} | |
// seems like these need to be delcared explicitly? | |
extension UICollectionViewController: CollectionViewContainer { } | |
// without this, compiler complains that UICollectionViewController has no member "visibleCells" | |
extension UICollectionViewController: CellContainer { } | |
let collectionVC = UICollectionViewController(collectionViewLayout: UICollectionViewFlowLayout()) | |
collectionVC.visibleCells |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment