Objective-C Block方法与笑笑语法
#pragma mark - block语法
NSLog(@"========block方法1===========");
[students sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Student *stu1 = (Student *)obj1;//这里的obj1就是Student类型的
Student *stu2 = (Student *)obj2;
NSLog(@"%ld",[stu1.name compare:stu2.name]) ;
return [stu1.name compare:stu2.name];
}];
NSLog(@"=======方法2==========");
NSComparator sortByName = ^NSComparisonResult(Student *obj1,Student *obj2)
{
return [obj1.name compare:obj2.name];
};
[students sortUsingComparator:sortByName];
NSLog(@"%@",students);
//年龄
NSLog(@"=========age=========");
[students sortUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
if (obj1.age > obj2.age) {
return NSOrderedDescending;
}else if (obj1.age < obj2.age)
{
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"%@",students);
//学号
NSLog(@"==========num=========");
[students sortUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
if (obj1.num > obj2.num) {
return NSOrderedDescending;
}else if (obj1.num < obj2.num)
{
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"%@",students);
#pragma mark - 笑笑语法
//笑笑语法获取字符串
NSString *str = [[NSString alloc] initWithUTF8String:"将C语言转化为OC字符串"];
NSString *str1 = @"笑笑语法";
//笑笑语法获取数组
NSArray *arr = [[NSArray alloc] initWithObjects:str,str1, nil];
// NSArray *arr1 = @[str,str1];
[arr objectAtIndex:0];
//arr[0];
NSLog(@"%@",arr[1]);
//笑笑语法 获取字典
NSLog(@"=======获取字典========");
//NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:str,@"str",str1,@"str1", nil];
NSDictionary *dic1 = @{@"str":str,@"str1":str1};
NSLog(@"%@",dic1);
//笑笑语法 通过key 获取value
[dic1 valueForKey:@"str1"];
NSLog(@"%@",dic1[@"str1"]);
//笑笑语法 获取值对象
NSNumber *num10 = [NSNumber numberWithInt:10];
@10;
@1.2;
NSLog(@"%@",num10);
//笑笑语法所创建的,都是不可变的.
}
return 0;
}