【问题标题】:How do I initialize variables using self.frame in init(size:)如何在 init(size:) 中使用 self.frame 初始化变量
【发布时间】:2020-06-22 10:36:10
【问题描述】:

我的class 需要一个init(size:) constructor 用于initialization,并且需要在我的variables 之后调用super.init(size:size)。这是有问题的,因为我想用size 初始化我的variables。如果我试试这个:

override init(size: CGSize) {
    tileWidth = self.frame.width/8
    tileHeight = tileWidth/2
    super.init(size: size)
 }

...然后我得到一个error

“self”在“super.init”调用之前用于属性访问“frame”

如果我这样做:

override init(size: CGSize) {
    super.init(size: size)
    tileWidth = self.frame.width/8 //140
    tileHeight = tileWidth/2
}

...我收到一条错误消息

属性 'self.tileHeight' 未在 super.init 调用时初始化

那么我该如何实现呢?

编辑 在其上下文中,代码如下所示:

class TileBoard: SKScene {

    let tileWidth:CGFloat
    let tileHeight:CGFloat


    override init(size: CGSize) {
        super.init(size: size)
        tileWidth = self.frame.width/8
        tileHeight = tileWidth/2
    }

    required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }

【问题讨论】:

  • 将它们设为可选?很难说你是否对我们隐瞒了类和超类的声明。

标签: swift dependency-injection initialization init


【解决方案1】:

考虑

class TopView:UIView {

    var tileWidth:CGFloat?
    var tileHeight:CGFloat?

    init(size: CGSize) {
        super.init(frame: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
     }

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

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class BottomView:TopView {

     override init(size: CGSize) {
        super.init(size: size)
        tileWidth = frame.width / 8.0
        tileHeight = tileWidth / 2.0
     }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    } 
}

【讨论】:

    【解决方案2】:

    我使用factory based dependency injection 修复了它,如下所示:

    protocol DependencyInjectionProtocol {
        func injection() -> CGFloat
    }
    
    class InjectWidth: UIViewController, DependencyInjectionProtocol {
        func injection() -> CGFloat {
            return self.view.frame.width/8.0
        }
    }
    
    class InjectHeight: UIViewController, DependencyInjectionProtocol {
        func injection() -> CGFloat {
            let height = (self.view.frame.width/8.0) / 2
            return height
        }
    }
    
    class InjectionClient {
        let delegate: DependencyInjectionProtocol
    
        init(delegate: DependencyInjectionProtocol) {
            self.delegate = delegate
        }
    }
    
    class TileBoard: SKScene {
    
        let tileWidth:CGFloat = InjectionClient(delegate: InjectWidth()).delegate.injection()
        let tileHeight:CGFloat = InjectionClient(delegate: InjectHeight()).delegate.injection()
    
    ...
    
    

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      相关资源
      最近更新 更多