iOS开发之单例

来源:互联网 发布:结构体包含数组memset 编辑:程序博客网 时间:2024/05/31 19:35

单例  内存中治存在一个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


学自刘凡老师

0 0
原创粉丝点击