从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什么的去赋值。
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!