1.block(代码块)
代码块对象简称“代码块”,其本质上是和其他变量类似,不同的时,代码块存储的数据是一个函数体,使用代码块的时候可以像其他函数一样,传入参数,并得到返回值。
代码块的语法格式:
(^blockname)(lsit of arguments) = ^(arguments){body;};
简单例子:void(^theBlock)()=^(){printf(“hello,qingyun”)};
代码块的使用方法:
1)直接调用: 如 theBlock();
2) 代码块内联:
NSArray *students=@[@”zhangsan”,@”lisi”,@”wangwu”,@”wangmazi”];
NSLog(@”unSorted students is :%@”,students);
//students调用sortedArrayUsingComparator方法,该方法有一个参数,这个参数为一个代码块的返回值,这个代码块的返回值为两个数的比较结果,是一个枚举类型 enum{ NSOrderedAscending = -1,NSOrderedSame,NSOrderedDescending}。方法的返回值为一个按升序排列的数组。
NSArray *sortedStudents = [students sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"Sorted students is :%@",sortedStudents);
还可以定义相同类型的block
如:
用typedef 声明了一个类型为int (^)(int number)类型的block,名字为quareBlock
typedef int (^quareBlock)(int number); quareBlock block1 = ^(int number){return number*number;};
int result1 = block1(3);
NSLog(@”%d”,result1);
关于代码块的变量
①局部变量:对于本地(局部)变量,放在栈区,修改其值不会影响到代码块内的变量的值
typedef double (^qySampleMultiplyBlockRef)(void);
double a = 10 ,b = 20;
//代码块在初始化的时候会捕获创建点时的状态
qySampleMultiplyBlockRef mutiply = ^{ return a * b;};
NSLog(@”%f”,mutiply());//结果为200
a = 20;b = 30;
NSLog(@"after modify a and b ,the result is %f",mutiply());//结果还是200
②全局变量(还有全局变量):对于全局变量,修改全局变量的值,会影响到代码块内部的变量的值
static double a = 10 ,b = 20;
qySampleMultiplyBlockRef mutiply = ^{
return a * b;
};
NSLog(@"%f",mutiply());//结果为200
NSLog(@"%f",ret);
a = 20;b = 30;
NSLog(@"after modify a and b ,the result is %f",mutiply());//结果为600
③__block标示符:对于局部变量,在block内在编译阶段会把它当做一个常量,如果想修改代码块内部的局部变量的值,用__block标识该变量。
double a = 10 ,b = 20;
__block double ret;
qySampleMultiplyBlockRef mutiply = ^{
ret = a * b;
return ret;
};
NSLog(@"%f",mutiply());//结果为200
NSLog(@"%f",ret);//结果为200
2.NSDate(日期类)
/****************将日期类型转化为字符串***********/
//创建一个当前的日期对象
NSDate *nowDate = [NSDate date];
NSLog(@”%@”,nowDate);
//创建一个日期转化器
NSDateFormatter *dateFormatter = [[ NSDateFormatter alloc]init];
//用日期转化器设定要转化的类型
[dateFormatter setDateFormat:@"yyyy/MM/d HH:mm:ss EEEE Z"];
//将日期转化为响应的字符串
NSString *strDate = [dateFormatter stringFromDate:nowDate];
NSLog(@"%@",strDate);
/****************将字符串类型转化为日期类型********/
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@”yyyy/MM/dd HH:mm”];
NSDate *date = [formatter dateFromString:@"14/12/05 15:20"];
NSLog(@"%@",date);
其中 setDateFormat 后面的字符串参数可以根据自己的需求去设定,其代表意思如下:
3.NSData
在OC中,将字符串、数组、字典等对象写到文件中去,十分方便。主要用法如下:
1)系统对象:
①NSArray:
NSArray *arr = @[@”zhangsan”,@”lisi”,@”wangwu”,@”zhangmazi”];
//创建一个文件路径。
NSString *homePah = [@”~/array.txt” stringByExpandingTildeInPath];
//将数组的内容写入到指定的文件中
[arr writeToFile:homePah atomically:YES];
//将指定的文件中的内容写到数组中
NSArray *resultArray = [[NSArray alloc ]initWithContentsOfFile:@"/users/qingyun/array.txt"];
NSLog(@"%@",resultArray);
②NSDictionary:
//创建一个文件路径
NSString *homepath = [@”~/dicResult.txt” stringByExpandingTildeInPath];
NSDictionary *dic = @{@”zhangsan”: @”张三”,@”lisi”:@”李四”,@”wangwu”:@”王五”};//初始化一个字典
//将该字典写入直接的文件下
[dic writeToFile:homepath atomically:YES];
NSDictionary *getResult = [[NSDictionary alloc] initWithContentsOfFile:homepath];
NSLog(@"%@",getResult);
2)非系统类对象,我们手动创建的对象
对于我们手动创建的对象,不能直接将这些对象内容写到文件中,需要编码、解码然后再序列化、反序列化才能去写到文件中,或从文件中读出来。
比如我们手动创建的对象student,并初始化
- (id)init
{
self = [super init];
if (self) {
self.name = @”zhangsan”;
self.age = 23;
self.stuNo = @”410423”;
}
return self;
}
①编码、解码
//编码
- (void)encodeWithCoder:(NSCoder *)acoder
{
[acoder encodeObject:self.name forKey:kKeyName];
[acoder encodeInt64:self.age forKey:kKeyAge];
[acoder encodeObject:self.stuNo forKey:kKeyStuNo];
}
//解码
- (id)initWithCoder:(NSCoder *)acoder
{
self = [super init];
if (self) {
self.name = [acoder decodeObjectForKey:kKeyName];
self.age = [acoder decodeInt64ForKey:kKeyAge];
self.stuNo = [acoder decodeObjectForKey:kKeyStuNo];
}
return self;
}
②序列化、反序列化
Student *stu = [[Student alloc]init];//创建一个stu对象并初始化
[NSKeyedArchiver archiveRootObject:stu toFile:@”/Users/qingyun/students.txt”];//将对象编码、序列化并写到指定路径的文件中去
NSString *strStu = [NSKeyedUnarchiver unarchiveObjectWithFile:@”/Users/qingyun/students.txt”];//从文件中读取并反序列化、解码成字符串
NSLog(@”3.%@”,strStu);//打印出来结果
总结:对于系统类的对象,可以直接将对象写入到指定的文件中去。对于我们手动创建的类对象,需要将对象序列化、反序列化后才能从文件中读取,在序列化、反序列化的过程中,需要先将对象编码、解码操作。