创建单例

来源:互联网 发布:安卓办公软件 编辑:程序博客网 时间:2024/05/17 22:17

static MyVC *_sharedInstance =nil;//可以是vc,也可以是obj    


@implementation MyVC

//第一种

+ (instancetype)sharedInstance

{

    staticdispatch_once_t once;//加锁

    dispatch_once(&once, ^{

        _sharedInstance = [[MyVCalloc] init];

        [_sharedInstanceview];

    });

    return_sharedInstance;

}

//第二种 不建议使用

+(instancetype)sharedMyReuqest

{

    @synchronized (self){

        if (!_sharedInstance) {

          _sharedInstance= [[MyVC alloc]init];

        }

        return _sharedInstance;

    }

}





0 0