• 从Objective-C到Swift,你必须会的(三)init的顺序


    Objective-C的构造函数吧,就最后return一个self。里头你要初始化了什么都可以。在Swift的init函数里把super.init放在前面,然后再初始化你代码里的东西就会报错了。

    所以:

    init(frame: NSRect) {
        super.init(frame: frame)
        subviewGroup = GridViewGroup(rows: 9, columns: 9, gridView: self)
    }
    
    /***  Properties  ***/
    let subviewGroup: GridViewGroup
    

     是不对的。

    应该是什么样的呢:

    init(frame: NSRect) {
            subviewGroup = GridViewGroup(rows: 9, columns: 9, gridView: self)
            super.init(frame: frame)
    }
    

     具体到UITableView的时候:

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
            self.tableView = UITableView(frame: CGRectMake(0, 0, CGRectGetWidth(rect), CGRectGetHeight(rect)), style: UITableViewStyle.Plain)
            
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
            dlog()
            self.view.addSubview(self.tableView)
    
            self.tableView.delegate = self
    
            self.tableView.dataSource = self
    
            self.tableView.allowsSelection = true
    
        }
    

     在super.init之后才能用self给delegate、datasource什么的去赋值。

  • 相关阅读:
    严格模式
    排序,求最大值最小值
    原型继承
    android 系统
    BASE64Decoder
    jstl
    list  遍历
    2015GitWebRTC编译实录9
    2015GitWebRTC编译实录8
    2015GitWebRTC编译实录7
  • 原文地址:https://www.cnblogs.com/sunshine-anycall/p/4036932.html
Copyright © 2020-2023  润新知