Block的使用
来源:互联网 发布:中国钢铁进出口数据 编辑:程序博客网 时间:2024/06/07 07:32
Main.m
//
// main.m
// 05-block
//
// Created by Apple on 14/12/2.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CZPerson.h"
/*
block 是一种数据类型,封装代码
函数不能在方法内部或函数内部,但是block可以
返回值类型
void (*指针名称)();
定义block类型的变量的格式
返回值类型 (^block变量名称)(形参列表);
实现格式
^ 返回值类型 (形参列表){ 语句..... };
*/
void test()
{
// 定义一个无参无返回值block类型的变量
void (^block)() = ^{
NSLog(@"testBlock");
};
block();
// 有参无返回值
void (^block1)(NSString *name) = ^(NSString *name){
NSLog(@"%@",name);
};
block1(@"大木");
// 有参有返回值
int (^sum) (int num1,int num2) = ^(int num1,int num2){
return num1 + num2;
};
int rs = sum(10,30);
NSLog(@"%d",rs);
}
void test3()
{
CZPerson *person = [[CZPerson alloc] init];
[person test:^{
NSLog(@"哈哈");
}];
int avr = [person average:^int(int num1,int num2) {
return num1 * num2;
}];
NSLog(@"%d",avr);
}
void test4()
{
CZPerson *person = [[CZPerson alloc] init];
// [person day2];
[person day:^{
NSLog(@"约会");
}];
[person day:^{
NSLog(@"打豆豆");
}];
}
/*
1、在block中可以访问局部变量的
默认在block内部不能修改局部变量的值
如果想修改局部变量的值,要再定义这个变量的时候,在前面加上__block
*/
void test5()
{
// int num = 10;
__block int num = 10;
// NSLog(@"%p",&num);
void (^test)() = ^{
num = 20;
// NSLog(@"%p",&num);
NSLog(@"%d",num);
};
test();
NSLog(@"%d",num);
}
void test6()
{
// 当你在block内部使用对象的时候,那么block内部就对这个对象有一个强引用
CZPerson *person = [[CZPerson alloc] init];
void (^test)() = ^{
[person day1];
};
person = nil;
}
//typedef 返回值类型 (^block类型名称)(参数列表);
typedef void (^voidType)();
int main(int argc,const char * argv[]) {
@autoreleasepool {
voidType test = ^{
NSLog(@"test");
};
test();
}
return0;
}
CZPerson。m
//
// CZPerson.m
// 1202-分类
//
// Created by Apple on 14/12/2.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "CZPerson.h"
@implementation CZPerson
- (void) test:(void (^)()) block
{
NSLog(@"test");
block();
}
- (int) average:(int (^)(int num1,int num2)) sum
{
return sum(10,20) /2;
}
/***程序员的一天***/
//工作准备
- (void) preWork{
NSLog(@"起床");
NSLog(@"刷牙");
NSLog(@"坐公交");
NSLog(@"打卡");
}
//下班后
- (void) afterWork{
NSLog(@"聊QQ");
NSLog(@"打卡");
NSLog(@"坐公交");
NSLog(@"刷牙");
NSLog(@"睡觉");
}
- (void) day1
{
[self preWork];
NSLog(@"了解需求");
[self afterWork];
}
- (void) day2
{
[self preWork];
NSLog(@"看代码");
[self afterWork];
}
- (void) day3
{
[self preWork];
NSLog(@"改个小bug");
[self afterWork];
}
- (void) day4
{
[self preWork];
NSLog(@"小功能");
[self afterWork];
}
- (void) day:(void (^)()) work
{
[self preWork];
work();
[self afterWork];
}
- (void)dealloc
{
NSLog(@"%s",__func__);
}
@end
//
// CZPerson.h
// 1202-分类
//
// Created by Apple on 14/12/2.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
/*
作为函数参数类型的格式
返回值类型(^)(形参列表)
当你发现,多个方法中他们有很多代码都是相同,只有一部分代码是经常变化
你可以把这个经常变化的代码通过block参数传入
*/
@interface CZPerson : NSObject
- (void) test:(void (^)()) block;
- (int) average:(int (^)(int num1,int num2)) sum;
/***程序员的一天***/
- (void) day1;
- (void) day2;
- (void) day3;
- (void) day4;
//通过block来重构它
- (void) day:(void (^)()) work;
@end
- Block的使用 iphone
- block的简单使用
- oc block的使用
- block的使用
- Block的使用详解
- Block使用的注意
- Block的使用
- Block语法的使用
- SDWebImage的block使用
- Block语法的使用
- block的使用
- block 的使用
- 关于Block的使用
- OC--Block的使用
- iOS block的使用
- block的使用
- block的使用
- ios block 的使用
- java代码优化浅析
- (8)利用JConsole工具监控java程序内存和JVM
- 越狱相关四:iPhone修改位置信息
- iscrll.js ,新手入门,学习笔记。
- 执行apt-get install 报错:Errors were encountered while processing
- Block的使用
- 解决IE8使用jquery设置网页标题title报错
- Avoiding Buffer Overruns
- 【6月英语学习总结】--Baby需要被关爱
- redis主从配置不生效问题
- oracle帐号scott被锁定如何解锁
- 遍历map的方法
- Python Scrapy爬虫,整站爬取妹子图
- asp 文档缓存尚在构建中