在程序执行中只执行一次指定处理的api, 使用dispatch_once 生成单例
#import <Foundation/Foundation.h>
@interface singleobj : NSObject<NSCopying>
+(singleobj*)sharedSingle;
@end
#import "singleobj.h"
@implementation singleobj
static singleobj* onceObj;
+(singleobj*)sharedSingle{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
onceObj=[[singleobj alloc] init];
});
return onceObj;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
onceObj=[super allocWithZone:zone];
});
return onceObj;
}
-(id)copyWithZone:(NSZone *)zone{
return onceObj;
}
@end