zhsoft88
V2EX  ›  iOS

objc tips - 单例实现 pattern

  •  
  •   zhsoft88 · Jul 20, 2015 · 2203 views
    This topic created in 3983 days ago, the information mentioned may be changed or developed.
    + (instancetype)sharedService {
      static id instance;
      static dispatch_once_t onceToken;
    
       dispatch_once(&onceToken, ^{
          instance = [self new];
        });
    
        return instance;
     }
    

    用id而不是实际的类名,用dispatch_once保证只调用一次,用[self new]创建对象,也不用实际的类名。

    pljhonglu
        1
    pljhonglu  
       Jul 20, 2015
    你在干嘛。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5852 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:13 · PVG 11:13 · LAX 20:13 · JFK 23:13
    ♥ Do have faith in what you're doing.