SingletonTool.h 文件
#import <Foundation/Foundation.h>
//遵守NSCopying,NSMutableCopying协议
@interface SingletonTool : NSObject<NSCopying,NSMutableCopying>
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age;
@property (nonatomic,assign) BOOL isBoy;
@property (nonatomic,strong) NSArray *inforarry;
//实例化一个类方法、返回一个单例对象
+ (instancetype)shareTools;
@end
//遵守NSCopying,NSMutableCopying协议
@interface SingletonTool : NSObject<NSCopying,NSMutableCopying>
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age;
@property (nonatomic,assign) BOOL isBoy;
@property (nonatomic,strong) NSArray *inforarry;
//实例化一个类方法、返回一个单例对象
+ (instancetype)shareTools;
@end
SingletonTool.m 文件
#import "SingletonTool.h"
//提供一个static修饰的全局变量、强引用已经实例化的对象
static SingletonTool *singTool;
@implementation SingletonTool
//实例化一个类方法、返回一个单例对象
+(instancetype)shareTools{
//注意:这里建议使用self,而不是直接使用类名Tools(考虑继承)
return [[self alloc] init];
}
//避免alloc产生新对象,所以需要重写allocWithZone方法
+(instancetype)allocWithZone:(struct _NSZone *)zone{
/*
1、使用GCD
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singTool = [super allocWithZone:zone];
});
*/
//1、使用加锁的方式、保证只分配一次存储空间
@synchronized (self) {
if (singTool == nil) {
singTool = [super allocWithZone:zone];
}
}
return singTool;
}
//copy 返回一个不可变对象
-(id)copyWithZone:(NSZone *)zone{
//若原对象是不可变对象,那么返回原对象,并将其引用计数加1;
//return [[self class] allocWithZone:zone];
//若原对象是可变对象,那么创建一个新的不可变对象,并初始化为原对象的值,新对象的引用计数为 1。
return singTool;
}
//mutableCopy 创建一个新的可变对象,并初始化为原对象的值,新对象的引用计数为 1;
-(id)mutableCopyWithZone:(NSZone *)zone{
return singTool;
static SingletonTool *singTool;
@implementation SingletonTool
//实例化一个类方法、返回一个单例对象
+(instancetype)shareTools{
//注意:这里建议使用self,而不是直接使用类名Tools(考虑继承)
return [[self alloc] init];
}
//避免alloc产生新对象,所以需要重写allocWithZone方法
+(instancetype)allocWithZone:(struct _NSZone *)zone{
/*
1、使用GCD
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singTool = [super allocWithZone:zone];
});
*/
//1、使用加锁的方式、保证只分配一次存储空间
@synchronized (self) {
if (singTool == nil) {
singTool = [super allocWithZone:zone];
}
}
return singTool;
}
//copy 返回一个不可变对象
-(id)copyWithZone:(NSZone *)zone{
//若原对象是不可变对象,那么返回原对象,并将其引用计数加1;
//return [[self class] allocWithZone:zone];
//若原对象是可变对象,那么创建一个新的不可变对象,并初始化为原对象的值,新对象的引用计数为 1。
return singTool;
}
//mutableCopy 创建一个新的可变对象,并初始化为原对象的值,新对象的引用计数为 1;
-(id)mutableCopyWithZone:(NSZone *)zone{
return singTool;
}
@end
//存入相应属性
SingletonTool *sing = [SingletonTool shareTools];
sing.name = @"Jace";
sing.age = 15;
sing.isBoy = YES;
sing.inforarry = [NSArray arrayWithObjects:@"123",@"abc",@"QWE", nil];
NSLog(@"1-> sing.name = %@, sing.age = %d, sing.isBoy = %d,sing.inforarry = %@", sing.name,sing.age,sing.isBoy, sing.inforarry);
SingletonTool *sing = [SingletonTool shareTools];
sing.name = @"Jace";
sing.age = 15;
sing.isBoy = YES;
sing.inforarry = [NSArray arrayWithObjects:@"123",@"abc",@"QWE", nil];
NSLog(@"1-> sing.name = %@, sing.age = %d, sing.isBoy = %d,sing.inforarry = %@", sing.name,sing.age,sing.isBoy, sing.inforarry);
//取出相应值
SingletonTool *sing = [SingletonTool shareTools];
NSLog(@"3-> sing.name = %@, sing.age = %d, sing.isBoy = %d,sing.inforarry = %@", sing.name,sing.age,sing.isBoy, sing.inforarry);
NSLog(@"3-> sing.name = %@, sing.age = %d, sing.isBoy = %d,sing.inforarry = %@", sing.name,sing.age,sing.isBoy, sing.inforarry);