#import <Foundation/Foundation.h>
#import "Calculate.h"
//数据类型
//1.作为参数传递
//2.作为函数的返回值
//3.声明成变量
void test()
{
NSLog(@"test");
}
int sum(int a, int b)
{
return a + b;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
//int 4 float double 8 char
//更加合理的分配内存空间
int ca =10;
//对象类型 NSObject * obj
//id
//BOOL
//block 指向函数的指针比较像
//SEL
/*
void (*myPoint)() = test;
myPoint();
*/
//block就是弥补了 指向函数的指针,不能够直接保存一个函数体(代码块)
/*
void (^myBlock)() = ^{
NSLog(@"test");
};
myBlock();
int (^sumBlock)(int a, int b) = ^int (int a, int b) {
return a + b;
};
int result = sumBlock(10,20);
NSLog(@"result = %d",result);
*/
//如果想要改变,block代码块之外的变量值,就必须在变量前加入
//__block关键字
__block int x = 0;
int (^sumBlock)(int a, int b) = ^int (int a, int b) {
int result = (a * b);
x = result;
return result;
};
//当在开发中,你发现一个方法中的算法,可以有多种实现,你一时还不能确定用哪种更好,你就可以吧方法中其中一个参数定义成block方式
//
Calculate * cal = [[Calculate alloc] init];
int sum = [cal calculateWithNumber1:10 andNumber2:20 andCalculate:sumBlock];
NSLog(@"sum = %d",sum);
NSLog(@"x = %d",x);
}
return 0;
}
#import "Calculate.h"
@implementation Calculate
/*
- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2
{
// return number1 + number2;
// return number1 - number2;
// return number1 * number2;
// return number1 / number2;
}
*/
- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate
{
//经常变化的功能,在设计当中叫做封装变化
return calculate(number1,number2);
}
@end
#import <Foundation/Foundation.h>
typedef int (^calculateBlock)(int a,int b);
@interface Calculate : NSObject
- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate;
@end
/*
SEL数据类型是用来包装方法的
*/
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person * p = [[Person alloc] init];
//消息机制
// [p eat];
/*
//使用@selector就能够把一个方法包装成 SEL数据类型
SEL s1 = @selector(eat);
[p performSelector:s1];
SEL s2 = @selector(call:);
[p performSelector:s2 withObject:@"135047654"];
SEL s3 = @selector(findName);
NSString * str = [p performSelector:s3];
NSLog(@"%@",str);
*/
// [p performSelector:@selector(eat)];
[p performSelector:@selector(abc)];
[p eat];
}
return 0;
}