stringCode stringCode - 3 months ago 39
iOS Question

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

UIScrollView has a delegate property witch 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?

}


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 (e.i. use custom delegate protocol) ?

Answer

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() {
    // ...
}