dispatch_once
来源:互联网 发布:淘宝营业执照怎么办理 编辑:程序博客网 时间:2024/06/10 20:25
dispatch_once的意思是程序在执行期间只执行一次dispatch_once包含的代码:
建一单视图应用程序:‘
// TestClass.h#import <Foundation/Foundation.h>@interface TestClass : NSObject- (void)test;@end
// TestClass.m#import "TestClass.h"@implementation TestClass- (void)test{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"This is a test."); });}@end
// ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
// ViewController.m#import "ViewController.h"#import "TestClass.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(20, 20, 280, 30)]; [button setTitle:@"Test" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];}- (void)buttonClick:(id)seneder{ TestClass *testObj = [[TestClass alloc] init]; [testObj test]; [testObj release];}@end
无论按钮按几次,This is a test. 只会打印一次
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- dispatch_once
- IOS dispatch_once
- IOS dispatch_once
- dispatch_once浅谈
- dispatch_once 实现单例
- dispatch_once创建单例
- @synchronized vs dispatch_once
- dispatch_once 优化代码性能
- dispatch_once创建单例
- dispatch_once创建单例
- dispatch_once优化代码性能
- dispatch_once创建单例
- dispatch_once 创建单例
- 关系数据库表主键和外键
- SURF源码分析之surflib.h
- Android实现推送方式解决方案
- 博客更新
- stl的unique简单demo
- dispatch_once
- 转换流
- 查找所有包含某个字符的序号
- 外部表(oracle)
- xls----->sql
- flex4 动态获取对象属性,动态生成数据源,动态生成lineChart,清除数据。
- Android弹出多选框下拉框的实例
- 求一个字符串中连续出现次数最多的子串
- HDU4432-第37届ACM/ICPC天津现场赛B题