iOS中的单例
2016-03-08 17:11
453 查看
//单例模式:是只有一个实例,而且自行实例化,并向整个程序提供这个实例
//优点:1.实例控制:singleton会阻止其他对象实例化它自己本身,也就是说在内存中单例只实例化一次.唯一实例
//2.灵活性:因为自己本身控制了实例化过程,所以可以更灵活的修改实例化过程.
.h文件
@property(strong,nonatomic)NSString *aString;
//单例 1.
+(instancetype)shareImageLoading;
.m文件
//1.定义一个全局变量(一定要赋值为nil)
@implementation Singleton
//单例对象的生命周期和整个程序一样长
//单例
+(instancetype)shareImageLoading
{
static ImageLoading *imageLoading = nil;
@synchronized(self) {
if (imageLoading == nil) {
static dispatch_once_t token;
dispatch_once(&token, ^{
imageLoading=[[ImageLoading alloc]init];
});
}
return imageLoading;
}
}
@end
//优点:1.实例控制:singleton会阻止其他对象实例化它自己本身,也就是说在内存中单例只实例化一次.唯一实例
//2.灵活性:因为自己本身控制了实例化过程,所以可以更灵活的修改实例化过程.
.h文件
@property(strong,nonatomic)NSString *aString;
//单例 1.
+(instancetype)shareImageLoading;
.m文件
//1.定义一个全局变量(一定要赋值为nil)
@implementation Singleton
//单例对象的生命周期和整个程序一样长
//单例
+(instancetype)shareImageLoading
{
static ImageLoading *imageLoading = nil;
@synchronized(self) {
if (imageLoading == nil) {
static dispatch_once_t token;
dispatch_once(&token, ^{
imageLoading=[[ImageLoading alloc]init];
});
}
return imageLoading;
}
}
@end
相关文章推荐
- iOS事件传递和事件响应链
- iOS7编程Cookbook中例15.8中一个小问题
- iOS7编程Cookbook中例15.8中一个小问题
- iOS7编程Cookbook中例15.8中一个小问题
- iOS----------SDWebimage源码解析(4)
- iOS真机测试
- iOS中的单例设计模式详解
- iOS测试证书创建,真机测试
- ios项目打包上线
- iOS 开发实践之 Auto Layout
- iOS拨打电话方式
- iOS程序支持64位以及project.pbxproj文件的介绍
- 通过URL Scheme传递参数
- IOS动画教程(一)
- IOS关于Runtime
- iOS 七巧板动画
- ios 点击抽屉页面按钮,触发跳转另一个页面
- project.pbxproj 文件的学习
- ios时间获取刚刚几分钟几秒钟几小时之前
- ios键盘高度监听