单例 内存中治存在一个instance 用处广泛,音频 电影 游戏等等
上代码
.h文件中
//
// DemoObj.h
// xc_单例
//
// Created by feishun on 14-11-13.
// Copyright (c) 2014年 feishun. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DemoObj : NSObject
+(instancetype)sharedDemoObj;
@end
.m文件中
//
// DemoObj.m
// xc_单例
//
// Created by feishun on 14-11-13.
// Copyright (c) 2014年 feishun. All rights reserved.
//
#import "DemoObj.h"
@implementation DemoObj
+(id)allocWithZone:(struct _NSZone *)zone
{
static DemoObj* instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
instance = [super allocWithZone:zone];
});
return instance;
}
+(instancetype)sharedDemoObj
{
return [[self alloc] init];
}
@end