iOS开发中常用的单例

定义:一个类的对象,无论在何时创建、无论创建多少次,创建出来的对象都是同一个对象。
使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中。
 
关键代码
+ (instancetype)allocWithZone:(struct_NSZone *)zone
{
    static id instance = nil;
    if(instance == nil)
    {
      instance =   [super allocWithZone:zone];
    }
    return instance;
}

 

1、UIApplication(应用程序实例)

获取方式:[UIApplication sharedApplication]

详细:http://www.cnblogs.com/hissia/p/5678518.html

 

2、NSNotificationCenter(消息中心)

获取方式:[NSNotificationCenter defaultCenter]

常用的通知模式

 

3、NSFileManager(文件管理)

获取方式:[NSFileManager defaultManager]

 

4、NSUserDefaults(偏好设置)

获取方式:[NSUserDefaults standardUserDefaults]

详细:http://www.cnblogs.com/hissia/p/5642405.html

 

5、NSURLCache(请求缓存)

获取方式:[NSURLCache sharedURLCache]

 

6、NSHTTPCookieStorage(应用程序cookies池)

获取方式:[NSHTTPCookieStorage sharedHTTPCookieStorage]

 

7、NSURLSession(发送请求时候用的)                

获取方式:[NSURLSession sharedSession]

 

8、UIMenuController(弹出的菜单可以选择,复制,剪切,粘贴的功能)      

获取方式:[UIMenuController sharedMenuController]

详细:http://www.cnblogs.com/hissia/p/5668513.html

posted @ 2016-08-03 20:47  ys123456789  阅读(266)  评论(1)    收藏  举报