Search code examples
swiftmoduleoverridingvarinitializer

Changing the initial value of a var in another module


I am using this module to display an image: https://github.com/huynguyencong/ImageScrollView/blob/master/Sources/ImageScrollView.swift

I have added it to my project using CocoaPods and I want to change the value of the var "maxScaleFromMinScale". I just cant figure out how.

First I tried to just override the var which is not possible.

I then tried to override the intializers by just copying and overriding the same ones that are overrided by the creator of the module.

override init(frame: CGRect) {
    super.init(frame: frame)
}

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

That does not work, it gives me an error on the top initializer saying that I am not overriding the designated initializer. It feels like I cannot override it since it is not public like the second one. Correct?

Last thing I tried was to make an extension:

extension ImageScrollView {
    public func setMaxScale(scale: CGFloat) {
        maxScaleFromMinScale = scale
    }
}

That does not work either, maxScaleFromMinScale is an unresolved identifier. This also seems to be because it is an internal var and I do not have access to it (another module). Does this mean that my ONLY option is to copy the whole file and modify the source. Was hoping for a minimal and elegant solution.

If this is the case, why can you override the built in UIKit elements but not this one that I downloaded using CocoaPods.


Solution

  • Yes, you cannot override the variable/function because it's internal. That's the point of access modifiers.

    Certain UIKit functions are public so you can override them.