您的位置:首页 > 移动开发 > IOS开发

iOS 单例

2016-02-21 19:28 435 查看
//

//  Singleton.m

//  Singleton

//

//  Created by fujiangren on 16/2/21.

//  Copyright © 2016年 fujiangren. All rights reserved.

//

#import "Singleton.h"

@implementation Singleton

+(Singleton *)sharedInstance{

    if (!_sharedInstance) {

        _sharedInstance = [[self
alloc] init];

    }

    return
_sharedInstance;

}

// 避免生成两个_sharedInstance,仅在多线程下需要这样做

+(id) allocWithZone:(struct
_NSZone *)zone{

    

    @synchronized(self)

    {

        if(_sharedInstance ==
nil){

            _sharedInstance = [super
allocWithZone:zone];

            return
_sharedInstance;

        }

    }

    return
nil;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios