zhsoft88
V2EX  ›  iOS

objc tips - 单例实现 pattern

  •  
  •   zhsoft88 · Jul 20, 2015 · 2270 views
    This topic created in 4017 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   ·   2917 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 13:19 · PVG 21:19 · LAX 06:19 · JFK 09:19
    ♥ Do have faith in what you're doing.