Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding delegate property of UIScrollView in Swift (like UICollectionView does)

UIScrollView has a delegate property which conforms to UIScrollViewDelegate

protocol UIScrollViewDelegate : NSObjectProtocol {     //... } class UIScrollView : UIView, NSCoding {     unowned(unsafe) var delegate: UIScrollViewDelegate?     //... } 

UICollectionView overrides this property with a different type UICollectionViewDelegate

protocol UICollectionViewDelegate : UIScrollViewDelegate, NSObjectProtocol {    //... }  class UICollectionView : UIScrollView {      unowned(unsafe) var delegate: UICollectionViewDelegate?    //... } 

When I try to override UIScrollViews delegate with my protocol like so:

protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol {     //... }  class MyScrollView: UIScrollView {     unowned(unsafe) var delegate: MyScrollViewDelegate?  } 

the compiler gives me two warnings:

  • Property 'delegate' with type 'MyScrollViewDelegate?' cannot override a property with type 'UIScrollViewDelegate?'
  • 'unowned' cannot be applied to non-class type 'MyScrollViewDelegate?'

How can I subclass UIScrollView and override type of delegate property (i.e. use a custom delegate protocol) ?

like image 433
stringCode Avatar asked Sep 08 '14 12:09

stringCode


2 Answers

I think overriding an inherited property is something that's possible in Objective-C but not (at least currently) in Swift. The way I've handled this is to declare a separate delegate as a computed property of the correct type that gets and sets the actual delegate:

@objc protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol {     func myHeight() -> CGFloat     // ... }  class MyScrollView: UIScrollView {     var myDelegate: MyScrollViewDelegate? {         get { return self.delegate as? MyScrollViewDelegate }         set { self.delegate = newValue }     } } 

This way anything that calls the scroll view delegate normally still works, and you can call your particular delegate methods on self.myDelegate, like this:

if let height = self.myDelegate?.myHeight() {     // ... } 
like image 92
Nate Cook Avatar answered Oct 17 '22 05:10

Nate Cook


You can do like this:

protocol ExtendedUIScrollViewDelegate: UIScrollViewDelegate {     func someNewFunction() }  class CustomScrollView: UIScrollView {      weak var myDelegate: ExtendedScrollViewDelegate?     override weak var delegate: UIScrollViewDelegate? {         didSet {             myDelegate = delegate as? ExtendedScrollViewDelegate         }     } } 

Hope this helps

like image 23
谢仪伦 Avatar answered Oct 17 '22 05:10

谢仪伦