Zombie因Swift继承崩溃

时间:2018-03-14 09:55:39

标签: ios objective-c swift uiview

我有Swift类(派生自UIView),我在Objective C ++代码中调用它。

最初,它运作良好,看起来像这样:

@objc
public class HorizonView: UIView {

    var dashGap: (CGFloat, CGFloat)? = nil

    let titleLayer = HorizonTextLayer()
    let angleLayer = HorizonTextLayer()
    let dashLayer = CAShapeLayer()

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

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

    public override var frame: CGRect {
        didSet {
            setUpLayers()
        }
    }
    // other stuff here ... 
}

Objective C ++调用alloc view:

_horizonView = [[HorizonView alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
[bigView addSubview:_horizonView];

Objective C ++调用删除视图:

if ( _horizonView ) { [_horizonView removeFromSuperview]; [_horizonView release]; _horizonView = nil; }


我决定通过只暴露一个公共方法来封装Horizo​​nView。我不知道是否可以声明protocol,它是UIView的子类(协议isn' ta类,是吗?),所以我来上课:

@objc
public class HorizonView: UIView {
    static func create(p1: CGPoint, andP2 p2: CGPoint) -> HorizonView {
        let implFrame = CGRect(x: p1.x, y: p1.y-10, width: p2.x-p1.x, height: 20)
        let impl = HorizonViewImpl(frame: implFrame)
        return impl
    }
}

private class HorizonViewImpl: HorizonView {

    var dashGap: (CGFloat, CGFloat)? = nil

    let titleLayer = HorizonTextLayer()
    let angleLayer = HorizonTextLayer()

    // old class code ...
} 

现在Objective C ++ alloc调用看起来如此:

if ( _horizonView ) { [_horizonView removeFromSuperview]; [_horizonView release]; _horizonView = nil; }
_horizonView = [HorizonView createWithP1:CGPointMake(10, 10)
                                   andP2:CGPointMake(210,40)];
[EGLV addSubview:_horizonView]; 


在分析时,我经常收到崩溃,看到这个: enter image description here

你能提供一些提示来检测问题吗? 这种继承方法有可能存在一些缺陷吗?

0 个答案:

没有答案