当前位置: 首页 > news >正文

一次性代码(单例)

 

//  CZTool.h

一次性代码

#import <Foundation/Foundation.h>

 

@interface CZTool : NSObject

 

// 用单例设计模式,可以节省内存.

 

// 书写单例

// 1. 对外提供一个获取单例对象的接口(API)

 

+(instancetype)sharedCZTool;

 

@end

 

 

//  CZTool.m

一次性代码

 

#import "CZTool.h"

 

@implementation CZTool

 

// 定义一个全局的变量来接受创建好的单例对象.

// 目的:为了使其他方法再次创建对象的时候,直接返回这个单例对象

 

static id _instance;

 

+(instancetype)sharedCZTool

{

    // 一次性代码中的代码,只能够被执行一次.

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        _instance = [[self alloc] init];

 

    });

    

  //  _instance = [[self alloc] init];

    

    return _instance;

}

 

 

// 当调用 alloc init 方法之后,内部都会调用这个方法.

//+(instancetype)allocWithZone:(struct _NSZone *)zone

//{

//    static dispatch_once_t onceToken;

//    dispatch_once(&onceToken, ^{

//        

//        _instance = [super allocWithZone:zone];

//        

//    });

//    

//    return _instance;

//}

 

@end

 

 

//  ViewController.m

一次性代码

 

#import "ViewController.h"

#import "CZTool.h"

 

@interface ViewController ()

 

@property (nonatomic,assign) BOOL is_onceTime;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.is_onceTime = YES;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

 

    CZTool *tool1 = [CZTool sharedCZTool];

    CZTool *tool2 = [CZTool sharedCZTool];

    CZTool *tool3 = [CZTool sharedCZTool];

    CZTool *tool4 = [CZTool sharedCZTool];

    

    CZTool *tool = [[CZTool alloc] init];

 

    NSLog(@"%@,%@,%@,%@,%@",tool1,tool2,tool3,tool4,tool);

    

    

}

 

 

- (void)onceTime

{

    // 实际开发中,GCD提供了一次性代码的函数.是线程安全的一次性代码.

    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        

        [self test2];

    });

    

    [self performSelectorInBackground:@selector(test2) withObject:nil];

    

    

    [NSThread detachNewThreadSelector:@selector(test2) toTarget:self withObject:nil];

    

    [self test2];

}

 

// GCD中的一次性代码,需要掌握.

// 在写单例的时候,经常使用.

// 最简单的实现单例设计模式的方法.

- (void)test2

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        NSLog(@"一次性代码,只执行一次");

 

    });

}

 

- (void)isOnceTime

{

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        

        [self test];

    });

    

    [self performSelectorInBackground:@selector(test) withObject:nil];

    

    

    [NSThread detachNewThreadSelector:@selector(test) toTarget:self withObject:nil];

    

    [self test];

}

 

 

// 如果使用Bool值做判断,为了保证线程安全,必须加锁.

- (void)test

{

    @synchronized(self)

    {

        if (self.is_onceTime) {

        

        NSLog(@"一次性代码,只执行一次");

        self.is_onceTime = NO;

    }

        

}

    

 

    

}

 

@end

转载于:https://www.cnblogs.com/R-X-L/p/4780455.html

相关文章:

  • 前端面试之闭包
  • linux磁盘管理以及linux文件系统管理
  • 普通用户自动挂载光盘
  • SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
  • eclipse自动为变量生成Get/Set函数
  • 【Unity】Update()和FixedUpdate()
  • python标识符
  • Ubuntu 12.04 怎样安装 Google Chrome
  • Xampps 1.9.1 系列版本正式发布
  • SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
  • Jfreechart绘制漂亮的图表
  • Linux TC基于HTB队列的流量管理范例
  • android 引导页设置(只让activity运行一次)
  • nyoj 214 单调递增子序列(二) 【另类dp】
  • 阿里巴巴已拿下中国互联网半壁江山
  • CAP理论的例子讲解
  • echarts的各种常用效果展示
  • httpie使用详解
  • JavaScript 基础知识 - 入门篇(一)
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • Markdown 语法简单说明
  • Python利用正则抓取网页内容保存到本地
  • quasar-framework cnodejs社区
  • Rancher如何对接Ceph-RBD块存储
  • 闭包,sync使用细节
  • 浮现式设计
  • 检测对象或数组
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 前端设计模式
  • 区块链将重新定义世界
  • 使用 Docker 部署 Spring Boot项目
  • 思维导图—你不知道的JavaScript中卷
  • 学习笔记:对象,原型和继承(1)
  • 移动端唤起键盘时取消position:fixed定位
  • 最简单的无缝轮播
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • 交换综合实验一
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • #NOIP 2014# day.2 T2 寻找道路
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .cn根服务器被攻击之后
  • .NET Standard 支持的 .NET Framework 和 .NET Core