OC
- block类型实例变量:回传值(^名字)(参数列);
- typedef 回传值(^名字)(参数列);
- 实现体:^(参数列){} 如果没有参数可以省略参数列和圆括弧
- 形參类型:(回传值(^)(参数列))名字
- 返回一个block:(回传值(^)(参数列))名字:(参数类型)参数名,...
Swift
- block类型实例变量:名字:(参数列表)->(返回值)
- typedef 名字 = (参数列表)->(返回值)
- 实现体:{[捕获值列表](参数列表)in 语句}
- 形參类型:名字:(参数列表)->(返回值)
C 函数
- block类型实例变量: 回传值 (*名字)(参数列)
- typedef 回传值 (*名字)(参数列)
- 实现体: 好像没有带参数、返回值之类的匿名函数块
- 形參类型: 回传值(*名字)(参数列)
- 返回一个函数:回传值(*名字((参数类型)参数名1,...))(参数列)
arc问题
arc是引用类型的来说的。值类型的内存管理与arc无关。arc内存管理操作方法:可变拷贝, 不可变拷贝 const, 保持强引用, 保持弱引用
1.给类成员变量赋值
2.给临时变量赋值
3.函数传参
4.闭包捕获
5.作为函数/闭包的返回值返回
深拷贝,浅拷贝是另一个概念
参考:
-(void)test_7 {
NSMutableString *p2 = [NSMutableString stringWithString:@"111111111"];
void (^block)(NSString *p) = ^(NSString *p) {
NSLog(@"%@", p);
NSString *p3 = p2;
p3 = @"33333";
};
NSLog(@"%@", p2);
block(@"helloworld");
NSLog(@"%@", p2);
NSLog(@"test6 end!!");
}
-(void)test_6 {
NSMutableString *p2 = [NSMutableString stringWithString:@"111111111"];
void (^block)(NSString *p) = ^(NSString *p) {
NSLog(@"%@", p);
NSMutableString *p3 = p2;
[p3 appendString:@"2222222"];
};
NSLog(@"%@", p2);
block(@"helloworld");
NSLog(@"%@", p2);
NSLog(@"test6 end!!");
}
-(void)test_5 {
NSMutableString *p2 = [NSMutableString stringWithString:@"111111111"];
void (^block)(NSString *p) = ^(NSString *p) {
NSLog(@"%@", p);
[p2 appendString:@"2222222"];
};
NSLog(@"%@", p2);
block(@"helloworld");
NSLog(@"%@", p2);
NSLog(@"test5 end!!");
}
-(void)test_4 {
CLGeocoder *coder = [[CLGeocoder alloc]init];
CLLocation *locaiont = [[CLLocation alloc]initWithLatitude:39.2610740000 longitude:117.1818350000];
__block NSDictionary *result;
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[coder reverseGeocodeLocation:locaiont completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error);
} else {
for ( CLPlacemark * i in placemarks) {
NSLog(@"%@", i.addressDictionary);
result = i.addressDictionary;
break;
}
}
dispatch_group_leave(group);
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"%@", result );
});
NSLog(@"test 4end!!!!");
}
-(void)test_3 {
__block NSMutableString *p = [NSMutableString string];
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) , ^{
sleep(5);
[p appendString: @"hellworooldd!!!!"];
[p appendString: @"3333333!!!!"];
[p appendString: @"4444444!!!!"];
dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"%@", p );
});
NSLog(@"end!!!!");
}
-(void)test_2 {
__block NSString *p = @"helo";
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) , ^{
sleep(5);
dispatch_group_leave(group);
p = @"hellworooldd!!!!";
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"%@", p );
});
NSLog(@"end!!!!");
}
-(void)test_1 {
self.str = @"111111";
[self performSelectorOnMainThread:@selector(modifyStr) withObject:nil waitUntilDone:false];
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// NSLog(@"%@", self.str);
// sleep(3);
// NSLog(@"%@", self.str);
// });
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *p = self.str;
NSLog(@"%@", p);
sleep(3);
NSLog(@"%@", p);
}); NSLog(@"test end...");
}
-(void)modifyStr {
dot;
self.str = @"222222";
}
作为一个闭包,它需要具备的原则
1,block捕获值,不能被外面的修改而被修改。
2,block捕获值,修改捕获的值,外面可以用到被修改后的结果。