Frederick C. Lee Frederick C. Lee - 1 year ago 125
Objective-C Question

How would you effectively override a parent's computed iVar?

I'm trying to convert an existing Objective-C code into its Swift 3.0 equivalent.

I'm having trouble converting an Objective-C paradigm of defining the parent's declared getter (assessor) within its descendant; into its equivalent Swift 3.0's computed iVar paradigm.


Parent property:

@property(nonatomic, readonly) CGRect frameOfPresentedViewInContainerView;

Defining property within its descendant:

- (CGRect)frameOfPresentedViewInContainerView
CGRect containerViewBounds = self.containerView.bounds;
CGSize presentedViewContentSize = [self sizeForChildContentContainer:self.presentedViewController withParentContainerSize:containerViewBounds.size];

// The presented view extends presentedViewContentSize.height points from
// the bottom edge of the screen.
CGRect presentedViewControllerFrame = containerViewBounds;
presentedViewControllerFrame.size.height = presentedViewContentSize.height;
presentedViewControllerFrame.origin.y = CGRectGetMaxY(containerViewBounds) - presentedViewContentSize.height;
return presentedViewControllerFrame;

Swift 3.0

Parent property:

var frameOfPresentedViewInContainerView: CGRect { get }

When I attempt to define the value of 'frameOfPresentedViewInContainerView' via:

func frameOfPresentedViewInContainerView() -> CGRect {
let containerViewBounds = self.containerView?.bounds; // ...UIPresentationController - iOS 10


I get the following compiler error:

Method 'frameOfPresentedViewInContainerView()' with Objective-C
selector 'frameOfPresentedViewInContainerView' conflicts with getter
for 'frameOfPresentedViewInContainerView' from superclass
'UIPresentationController' with the same Objective-C selector

Which makes sense and is obvious.

So what's the best way to override a parent's computed (getter) variable?

Note: the parent class is Apple's canned UIPresentationController.

Answer Source

You override the var, just as given in the Swift interface:

override var frameOfPresentedViewInContainerView: CGRect {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download