Singleton in iOS Objective C doesn't prevent more than one instance

后端 未结 9 1550
长情又很酷
长情又很酷 2020-11-29 08:53

I know there are several threads on this, but none answer my questions.

I\'ve implemented my singleton class like this (being aware of the controversy about singleto

9条回答
  •  遥遥无期
    2020-11-29 09:30

    This works for me :

    static DataModel *singleInstance;
    
    + (DataModel*)getInstance{
        if (singleInstance == nil) {
            singleInstance = [[super alloc] init];
        }
        return singleInstance;
    }
    

    You can call it with

    _model = [DataModel getInstance];
    

提交回复
热议问题