標籤:
#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;
}
Objective-C Block方法與笑笑文法