Reputation: 1477
I have a small protocol defined in the following manner:
import UIKit
protocol HasMeterAnimation {
func animateMeter(scrollableView: UIScrollView)
}
It's implementation requires the use of IBOutlets:
func animateMeter(scrollableView: UIScrollView) {
let maxYPosTableview = scrollableView.frame.height + scrollableView.frame.origin.y
let progressBarRect = self.progressBar.frame
let rectOfProgressBarInParentView = self.convert(progressBarRect, to: scrollableView.superview)
let maxYPosMeter = rectOfProgressBarInParentView.origin.y + progressBarRect.height
if maxYPosTableview > maxYPosMeter {
if progressBar.isHidden == false {
progressBar.animateProgressBar()
model.shouldMeterAnimate = false
}
}
}
What I would like to know if there is a way for me to write this implementation in a protocol extension using the IBOutlets... something like this:
protocol HasMeterAnimation {
@IBOutlet var progressBar:AnimatedProgressBar! { get set }
var model: ListItem! { get set }
func animateMeter(scrollableView: UIScrollView)
}
extension HasMeterAnimation {
func animateMeter(scrollableView: UIScrollView) {
let maxYPosTableview = scrollableView.frame.height + scrollableView.frame.origin.y
let progressBarRect = self.progressBar.frame
let rectOfProgressBarInParentView = self.convert(progressBarRect, to: scrollableView.superview)
let maxYPosMeter = rectOfProgressBarInParentView.origin.y + progressBarRect.height
if maxYPosTableview > maxYPosMeter {
if progressBar.isHidden == false {
progressBar.animateProgressBar()
model.shouldMeterAnimate = false
}
}
}
I have tried many ways but keeps giving me errors. Thanks
Upvotes: 2
Views: 891
Reputation: 352
IBOutlet is just a keyword to let the interface builder know that you would like to reference an object from the interface builder in your class for example in your ViewController,View etc. In any other context IBOutlet does not make any sense. When you define properties in your protocol you also don't mark them as force unwrapped. When you don't mark a property explicitly as an optional then the property has to have a value at runtime, otherwise you can't compile your code. The interface builder marks its outlets as force unwrapped because they don't have values at compile time and are resolved at runtime.
Upvotes: 3