iOS7 Programming Cookbook 第七章学习笔记

####Accessing Variables in Block Objects
//访问block对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
typedef void (^BlockWithNoParams)(void);
- (void) scopeTest{
NSInteger integer = -101;
NSUInteger unsignedInteger = 101;

NSLog(@"测试32位,64位 展示 %zd %tu",integer,unsignedInteger);

__block NSUInteger integerValue = 10;

//创建
BlockWithNoParams myBlock = ^{
NSLog(@"Integer value inside the block = %lu",
(unsigned long)integerValue);
};

integerValue = 20;

/* 更改integerValue变量为20,初始为10 */
myBlock();

NSLog(@"Integer value outside the block = %lu",
(unsigned long)integerValue);
}

####Constructing Block Objects
//构建block对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//定义 返回字符串,传入NSUInteger
typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);

- (NSString *) convertIntToString:(NSUInteger)paramInteger
usingBlockObject:(IntToStringConverter)paramBlockObject{
return paramBlockObject(paramInteger);
}

//执行转换
- (void) doTheConversion{

NSString *result = [self convertIntToString:123 usingBlockObject:^NSString *(NSUInteger paramInteger) {
NSString *result = [NSString stringWithFormat:@"%lu",(unsigned long)paramInteger];
return result;
}];

NSLog(@"result = %@", result);

}

####Constructing Your Own Dispatch Queues with GCD
//在线程里构建调度队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* 1 */
void firstIteration(void *paramContext);
void secondIteration(void *paramContext);
void thirdIteration(void *paramContext);

/* 2 */
void firstIteration(void *paramContext){
NSUInteger counter = 0;
for (counter = 0;counter < 5; counter++){
NSLog(@"First iteration, counter = %lu", (unsigned long)counter);
}
}

void secondIteration(void *paramContext){
NSUInteger counter = 0;
for (counter = 0;counter < 5; counter++){
NSLog(@"Second iteration, counter = %lu", (unsigned long)counter);
}
}

void thirdIteration(void *paramContext){
NSUInteger counter = 0;
for (counter = 0; counter < 5; counter++){
NSLog(@"Third iteration, counter = %lu", (unsigned long)counter);
}
}

//创建一个新的调度队列模块
dispatch_queue_t firstSerialQueue = dispatch_queue_create("com.pixolity.GCD.serialQueue1", 0);
//函数的异步执行调度队列
dispatch_async_f(firstSerialQueue, NULL, firstIteration);
dispatch_async_f(firstSerialQueue, NULL, secondIteration);
dispatch_async_f(firstSerialQueue, NULL, thirdIteration);

####Creating Concurrency with Threads
//并发线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void) autoreleaseThread:(id)paramSender{

@autoreleasepool {
NSBundle *mainBundle = [NSBundle mainBundle];
//从resource读取图片
NSString *filePath = [mainBundle pathForResource:@"MacBookAir" ofType:@"png"];

UIImage *image = [UIImage imageWithContentsOfFile:filePath];

NSLog(@"Image = %@", image);
}

}

- (void)viewDidLoad {

[super viewDidLoad];
//另开线程读取图片
[NSThread detachNewThreadSelector:@selector(autoreleaseThread:)toTarget:self withObject:self];

}

####Creating Timers
//NSTimer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@interface AppDelegate ()
@property (nonatomic, strong) NSTimer *paintingTimer;
@end

- (void) paint:(NSTimer *)paramTimer{
/* Do something here */
NSLog(@"Painting");
}

- (void) startPainting{
//选择器调用
SEL selectorToCall = @selector(paint:);

//组成一个方法签名的选择器,我们知道选择器是当前类,所以它是很容易构造方法签名
//instanceMethodSignatureForSelector :返回一个NSMethodSignature对象,该对象包含一个实例的描述方法被给定的选择器。
NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selectorToCall];

//NSInvocation是一个objective - c消息呈现静态的,也就是说,它是一个行动变成了一个对象。NSInvocation对象用于存储和转发消息之间的对象和应用程序,主要由NSTimer对象和分布式对象系统。
//调用方法签名,安排一个计时器
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:self];
[invocation setSelector:selectorToCall];

//创建并返回一个新的NSTimer对象使用指定的调用初始化对象。
self.paintingTimer = [NSTimer timerWithTimeInterval:1.0
invocation:invocation
repeats:YES];;

//为安排计时器运行循环
[[NSRunLoop currentRunLoop] addTimer:self.paintingTimer
forMode:NSDefaultRunLoopMode];

}

//停止方法
- (void) stopPainting{
if (self.paintingTimer != nil){
//不为空,停止时间
[self.paintingTimer invalidate];
}
}

//后台
- (void)applicationWillResignActive:(UIApplication *)application{
[self stopPainting];
}

//进入前台
- (void)applicationDidBecomeActive:(UIApplication *)application{
[self startPainting];
}

####Exiting Threads and Timers
//NSThread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@property (nonatomic, strong) NSThread *myThread;

- (void) threadEntryPoint{

@autoreleasepool {
NSLog(@"线程入口");
while ([[NSThread currentThread] isCancelled] == NO){
//停4秒
[NSThread sleepForTimeInterval:4];
if ([[NSThread currentThread] isCancelled] == NO){
NSLog(@"线程循环");
}
}
NSLog(@"线程完成");
}

}


- (void) stopThread{
//取消线程
[self.myThread cancel];
self.myThread = nil;

}

//线程入口点
self.myThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(threadEntryPoint)
object:nil];
//停止线程
[self performSelector:@selector(stopThread) withObject:nil afterDelay:3.0f];

[self.myThread start];

####Grouping Tasks Together with GCD
//GCD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void reloadAllComponents(void *context);
- (void) reloadTableView{
NSLog(@"%s", __FUNCTION__);
}

- (void) reloadScrollView{
NSLog(@"%s", __FUNCTION__);
}

- (void) reloadImageView{
NSLog(@"%s", __FUNCTION__);
}

void reloadAllComponents(void *context){

AppDelegate *self = (__bridge AppDelegate *)context;

[self reloadTableView];
[self reloadScrollView];
[self reloadImageView];

}

//任务组线程
dispatch_group_t taskGroup = dispatch_group_create();
//主线程
dispatch_queue_t mainQueue = dispatch_get_main_queue();
//__bridge只做类型转换,不修改对象
dispatch_group_async_f(taskGroup,
mainQueue,
(__bridge void *)self,
reloadAllComponents);

//当我们完成,执行下面block
dispatch_group_notify(taskGroup, mainQueue, ^{
[[[UIAlertView alloc] initWithTitle:@"Finished"
message:@"All tasks are finished"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];

});

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%