iOS block 的 同步執行和非同步執行詳解 加 屬性字串 設定
阿新 • • 發佈:2019-01-23
直接上程式碼:在蘋果的api中的block有些是同步執行的block如:
array = [array sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) { // {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}; BOOL isBig = str1.integerValue < str2.integerValue; if (isBig) { return NSOrderedDescending; } return NSOrderedAscending; //NSOrderedAscending NSOrderedDescending }];
有些是非同步執行的block如:
//非同步執行的block
// dispatch_async(dispatch_get_main_queue(), ^{
// myBlock(12);
// });
全部示例程式碼:
// // ViewController.m // test_attribute_text // // Created by jeffasd on 16/5/17. // Copyright © 2016年 jeffasd. All rights reserved. // #import "ViewController.h" @interface ViewController () { NSArray *array; } @property (nonatomic, strong)UILabel *label; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // NSTextAttachment _label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)]; [self.view addSubview:_label]; NSTextAttachment *attach = [[NSTextAttachment alloc] init]; attach.image = [UIImage imageNamed:@"d_chanzui"]; attach.bounds = CGRectMake(0, -3, 15, 15); NSMutableAttributedString *emotionStr = [[NSAttributedString attributedStringWithAttachment:attach] mutableCopy]; NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:@"jjjjj"]; [emotionStr appendAttributedString:attributeStr]; _label.attributedText = emotionStr; NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary]; textAttrs[NSForegroundColorAttributeName] = [UIColor redColor]; textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:40]; [emotionStr addAttributes:textAttrs range:NSMakeRange(0, emotionStr.length)]; _label.attributedText = emotionStr; NSAttributedString *dictString = [[NSAttributedString alloc] initWithString:@"sdfsdf" attributes:textAttrs]; _label.attributedText = dictString; // _label.attributedText = textAttrs; array = @[@"123", @"32", @"56", @"78"]; array = [array sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) { // {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}; BOOL isBig = str1.integerValue < str2.integerValue; if (isBig) { return NSOrderedDescending; } return NSOrderedAscending; //NSOrderedAscending NSOrderedDescending }]; NSLog(@"the array is %@", array); // // array sor //同步執行的block [self blockTest:^(int var) { for (int i = 0; i < 9; i++) { NSLog(@"b"); } NSLog(@"var is %d", var); }]; NSLog(@"sdfsdf"); // //非同步執行的block // [self asyncBlockTest:^(int var) { // // for (int i = 0; i < 90; i++) { // NSLog(@"b"); // } // // NSLog(@"async var is %d", var); // // }]; // // NSLog(@"sdfsdf"); } //typedef <#returnType#>(^<#name#>)(<#arguments#>); - (void)blockTest:( void (^)(int) )myBlock{ //同步執行 // myBlock(12); //非同步執行的block // dispatch_async(dispatch_get_main_queue(), ^{ // myBlock(12); // }); [self performSelectorOnMainThread:@selector(asyncBlockAction:) withObject:myBlock waitUntilDone:NO]; } - (void)asyncBlockAction :( void (^)(int) )myBlock { myBlock(122); } - (void)asyncBlockTest:(void (^)(int))myAsyncBlock{ // NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // // [queue addOperationWithBlock:^{ // // NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; // [mainQueue addOperationWithBlock:^{ // myAsyncBlock(2323); // }]; // // // }]; NSOperationQueue *queue = [NSOperationQueue mainQueue]; [queue addOperationWithBlock:^{ // NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; // [mainQueue addOperationWithBlock:^{ // myAsyncBlock(2323); // }]; myAsyncBlock(2323); }]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSLog(@"the array is %@", array); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end