Singleton.h
////  Singleton.h//  SingletonDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import <Foundation/Foundation.h>@interface Singleton : NSObject+(instancetype)sharedInstance;@endSingleton.m
////  Singleton.m//  SingletonDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import "Singleton.h"@implementation Singleton+(instancetype)sharedInstance {    static Singleton *sharedInstance = nil;    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        sharedInstance = [[self alloc] init];    });    return sharedInstance;}@end上面这种方式是最安全也是最有效的创建单例的方式。不可能去创建两个实例,而且使100%的线程安全。
新闻热点
疑难解答