iOS 创建单例的方法

在ios4之后出现了一种单例的写法:

首先创建一个继承于NSObject的类

在.h文件中声明一个类方法

+ (InstallStatisticsClass *)shareInstance;

在.m中将这个方法

+ (InstallStatisticsClass *)shareInstance

{

    static InstallStatisticsClass *instance = nil;

    static dispatch_once_t str;

    dispatch_once (&str,^{

        instance = [[super allocWithZone:nil] init];

    });

    return instance;

}

posted @ 2014-11-12 15:46  暗夜追星  阅读(139)  评论(0编辑  收藏  举报