iOS开发学习笔记(OC语言)——block使用示例
//
// 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
分类:
iOS
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性