当前位置: 首页 > 工具软件 > iOS Block UI > 使用案例 >

iOS block 的 同步执行和异步执行详解 加 属性字符串 设置

尤飞尘
2023-12-01

直接上代码:在苹果的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


 类似资料: