[英]Swift Initialization - Why are multiple init functions being called?
這是我的班級覆蓋inits
class BottomView: UIView {
// MARK: - initilization
override init() {
println("init")
super.init()
setup()
}
override init(frame: CGRect) {
println("init frame")
super.init(frame: frame)
setup()
}
required init(coder aDecoder: NSCoder) {
println("init decoder")
super.init(coder: aDecoder)
setup()
}
func setup() {
println("setup")
}
}
然后我使用以下代碼在我的ViewController中初始化
class ViewController: UIViewController {
let bottomView = BottomView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
bottomView.frame = CGRectMake(0.0, self.view.frame.height/2.0, self.view.frame.width, self.view.frame.height/2.0)
bottomView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleTopMargin
self.view.addSubview(bottomView)
}
}
我的輸出是
init
init frame
setup
setup
所以我的問題是,為什么要調用init(frame :)?
super.init()
調用self.init(frame:...)
。 從本質上講,這只是他們說frame
是可選的方式; 如果你沒有傳遞它,它們會分配一個(0,0,0,0)
的幀。
func init() {
self.init(frame:CGRectMake(0,0,0,0));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.