objective-C学习 sigton单例
来源:互联网 发布:淘宝图片防止右键保存 编辑:程序博客网 时间:2024/05/17 09:02
#import <Foundation/Foundation.h>
#import "UserContext.h"
int main(int argc,const char * argv[]) {
@autoreleasepool {
UserContext *userContext = [UserContextshareUserContext];
UserContext *userContext2 = [UserContextshareUserContext];
UserContext *userContext3 = [[UserContextalloc]init];
UserContext *userContext4 = [userContextcopy];
UserContext *userContext5 = [userContextcopy];
[userContext retain];
[userContext release];
[userContext release];
[userContext release];
NSLog(@"");
}
return 0;
}
#import <Foundation/Foundation.h>
@interface UserContext : NSObject<NSCopying>
@property (nonatomic,copy)NSString *userName;
@property (nonatomic,copy)NSString *email;
+ (id)shareUserContext; //单例命名习惯前缀:share或default关键字
@end
#import "UserContext.h"
static UserContext *segtonInstance =nil;
@implementation UserContext
+ (id)shareUserContext
{
@synchronized(self) //线程保护
{
if (segtonInstance ==nil)
{
segtonInstance = [[[selfclass]alloc]init];
}
}
returnsegtonInstance;
}
//下面方法为了确保一个实例
+ (id)allocWithZone:(struct_NSZone *)zone{
if (segtonInstance ==nil) {
segtonInstance = [superallocWithZone:zone];
}
returnsegtonInstance;
}
- (id)copyWithZone:(nullableNSZone *)zone {
returnsegtonInstance;
}
- (id)retain{
returnsegtonInstance;
}
- (oneway void)release{
}
- (id)autorelease{
returnsegtonInstance;
}
-(NSUInteger)retainCount{
returnUINT_MAX;
}
@end
- objective-C学习 sigton单例
- Objective-C学习之创建单例
- Objective-C 单例
- Objective-C单例
- Objective-C学习备忘单
- Objective-C学习备忘单
- Objective-C学习备忘单
- Objective-C学习备忘单
- IOS学习笔记—Objective C—创建单例 .
- 黑马程序员 之 Objective-C 单例模式 学习记录
- Objective-C学习-单例以及通知中心(NotificationCenter)
- Objective-C单例实现
- Objective C--单例模式
- Objective C--单例模式
- Objective C 单例模式
- Objective-C 中的单例
- Objective C--单例模式
- Objective C 单例实现
- hadoop 部门职员的join 操作
- 20160205.CCPP体系详解(0015天)
- Jfire-mvc开发文档
- HaProxy 1.5 说明文档翻译
- BZOJ_P2761 [JLOI2011]不重复数字(sb题)
- objective-C学习 sigton单例
- 编程题#6:MyString(C++程序设计第10周)
- 第一届_第一题_黑色星期五
- 局部变量和全局变量
- c++虚拟继承
- Java基础10 接口的继承与抽象类
- 结构体
- Cygwin上Eclipsef运行Hadoop遇到的有关问题
- 编程题#7:字符串排序(C++程序设计第10周)