'super.init' isn't called on all

2021-03-22  本文已影响0人  90后的晨仔

前几天在用swift写自定义初始化方法的时候一下子给蒙了,今天总结一下。可以看看这篇介绍的详细

@interface JWeakAndStrongViewController : ViewController
- (instancetype)initWithVcName:(NSString *)vcName size:(NSInteger)size;
@end

@implementation JWeakAndStrongViewController

- (instancetype)initWithVcName:(NSString *)vcName size:(NSInteger)size{
    self = [super init];
    if (self) {
        
    }
    return self;
}


   
class JCBaseController: UIViewController {
  
    
    init(){
        super.init(nibName: nil ,bundle: nil)
           print("init()")
       }
       init(vcName: String ,size: Int) {
           super.init(nibName: nil, bundle: nil)
        print("name: + age")
       }
       convenience init(vcName : String) {
        self.init(vcName:"dada",size: 100)
       }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  }
    
class JCHomeViewController: JCBaseController {
 
    override func viewDidLoad() {
        let superVC1 = JCBaseController()
        let superVC2 = JCBaseController.init(vcName: "码农晨仔", size: 100)
        let superVC3 = JCBaseController.init(vcName: "码农晨仔")
    }
   }
上一篇 下一篇

猜你喜欢

热点阅读