iOS開發學習筆記(OC語言)——block使用示例
阿新 • • 發佈:2022-03-15
// // ViewController.m // ladder // // Created by xxx on 2022/3/9. // #import "ViewController.h" @interface ViewController() @property(nonatomic, strong, readwrite) NSTextField *infoLabel; @property(nonatomic, strong, readwrite) NSButton *infoBtn; @property(nonatomic, strong) dispatch_queue_t queue; typedef void (^writeProgress)(int progress); -(void)writeFile:(NSString *)fileName content:(NSString *)content progress:(writeProgress) progress; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _queue = dispatch_queue_create("rwqueue", DISPATCH_QUEUE_CONCURRENT); [self.view addSubview:({ self.infoLabel = [[NSTextField alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 80)]; self.infoLabel; })]; [self.view addSubview:({ self.infoBtn = [[NSButton alloc]initWithFrame:CGRectMake(10,self.infoLabel.frame.size.height + 20, self.view.frame.size.width - 20, 50)]; self.infoBtn.bordered = NO; self.infoBtn.wantsLayer = YES; self.infoBtn.layer.backgroundColor = [NSColor blueColor].CGColor; self.infoBtn.layer.cornerRadius = 5; [self setButtonTitleFor:self.infoBtn toString:@"Click Me" withColor:[NSColor whiteColor]]; [self.infoBtn setTarget:self]; [self.infoBtn setAction:@selector(_btnClick)]; self.infoBtn; })]; } - (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title withColor:(NSColor*)color{ NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:NSTextAlignmentCenter]; NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil]; NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:title attributes:attrsDictionary]; [button setAttributedTitle:attrString]; } -(void)_btnClick{ __weak __typeof(&*self)weakSelf = self; dispatch_async(_queue, ^{ [self writeFile:@"test.txt" content:@"Hello IOS!" progress:^(int progress) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"block current threadid: %@", [NSThread currentThread]); weakSelf.infoLabel.stringValue = [NSString stringWithFormat:@"progress: %d%%", progress]; }); }]; }); } -(void)writeFile:(NSString *)fileName content:(NSString *)content progress:(writeProgress) progress{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [paths firstObject]; NSFileManager *fileManager = [NSFileManager defaultManager]; //建立檔案 NSString *listDataPath = [cachePath stringByAppendingPathComponent:fileName]; [fileManager createFileAtPath:listDataPath contents:nil attributes:nil]; //查詢檔案 BOOL fileExist = [fileManager fileExistsAtPath:listDataPath]; if (fileExist) { //檔案追加內容 NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:listDataPath]; [fileHandle seekToEndOfFile]; NSMutableArray *listItems = [NSMutableArray array]; for (int i = 0; i < [content length]; i++) { NSString *ch = [content substringWithRange:NSMakeRange(i, 1)]; [listItems addObject:ch]; [listItems addObject:@"\n"]; } for (int i = 0; i < listItems.count; i++) { [fileHandle writeData:[listItems[i] dataUsingEncoding:NSUTF8StringEncoding]]; sleep(2); if (progress) { NSLog(@"current threadid: %@", [NSThread currentThread]); progress((int)((i+1)*1.0/listItems.count * 100)); } } [fileHandle synchronizeFile]; [fileHandle closeFile]; } } @end