您将如何有效地覆盖 parent 的计算 iVar?
How would you effectively override a parent's computed iVar?
我正在尝试将现有的 Objective-C 代码转换成它的 Swift 3.0 等效代码。
我在转换定义 parent 声明的 getter(评估者)在其后代中的 Objective-C 范式时遇到问题;进入其等效的 Swift 3.0 的 computed iVar 范式。
Objective-C
Parent 属性:
@property(nonatomic, readonly) CGRect frameOfPresentedViewInContainerView;
在其 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;
}
Swift3.0
Parent 属性:
var frameOfPresentedViewInContainerView: CGRect { get }
当我尝试通过以下方式定义 'frameOfPresentedViewInContainerView' 的值时:
func frameOfPresentedViewInContainerView() -> CGRect {
let containerViewBounds = self.containerView?.bounds; // ...UIPresentationController - iOS 10
}
我收到以下编译器错误:
Method 'frameOfPresentedViewInContainerView()' with Objective-C
selector 'frameOfPresentedViewInContainerView' conflicts with getter
for 'frameOfPresentedViewInContainerView' from superclass
'UIPresentationController' with the same Objective-C selector
这是有道理的,也是显而易见的。
那么覆盖 parent 的计算 (getter) 变量的最佳方法是什么?
注意:parent class 是苹果的罐装 UIPresentationController.
您重写 var,就像 Swift 界面中给出的那样:
override var frameOfPresentedViewInContainerView: CGRect {
我正在尝试将现有的 Objective-C 代码转换成它的 Swift 3.0 等效代码。
我在转换定义 parent 声明的 getter(评估者)在其后代中的 Objective-C 范式时遇到问题;进入其等效的 Swift 3.0 的 computed iVar 范式。
Objective-C
Parent 属性:
@property(nonatomic, readonly) CGRect frameOfPresentedViewInContainerView;
在其 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;
}
Swift3.0
Parent 属性:
var frameOfPresentedViewInContainerView: CGRect { get }
当我尝试通过以下方式定义 'frameOfPresentedViewInContainerView' 的值时:
func frameOfPresentedViewInContainerView() -> CGRect {
let containerViewBounds = self.containerView?.bounds; // ...UIPresentationController - iOS 10
}
我收到以下编译器错误:
Method 'frameOfPresentedViewInContainerView()' with Objective-C selector 'frameOfPresentedViewInContainerView' conflicts with getter for 'frameOfPresentedViewInContainerView' from superclass 'UIPresentationController' with the same Objective-C selector
这是有道理的,也是显而易见的。
那么覆盖 parent 的计算 (getter) 变量的最佳方法是什么?
注意:parent class 是苹果的罐装 UIPresentationController.
您重写 var,就像 Swift 界面中给出的那样:
override var frameOfPresentedViewInContainerView: CGRect {