gpt4 book ai didi

ios - setText 不适用于 UI 更新线程

转载 作者:行者123 更新时间:2023-11-28 22:30:21 24 4
gpt4 key购买 nike

我正在使用 https://github.com/mattconnolly/ZipArchive 循环解压缩文件.问题出在解压循环中,我无法更新我的标签。我的代码:

-(void) UnzipFiles
{

for (int i=0; i < zippedFiles.count; i++)
{

[lb7 setText:[NSString stringWithFormat:@"Unziping file %d/%d",i+1,zippedFiles.count]]; //not working


NSString *zipFilePath = [documentsDirectory stringByAppendingPathComponent:[zippedFiles objectAtIndex:i]];
NSString *output = [documentsDirectory stringByAppendingPathComponent:[unzipFolders objectAtIndex:i]];

NSLog(@"Unziping: %@ ",[zippedFiles objectAtIndex:i]);
NSLog(@"To: %@",[unzipFolders objectAtIndex:i]);


ZipArchive* za = [[ZipArchive alloc] init];

if( [za UnzipOpenFile:zipFilePath] ) {

if( [za UnzipFileTo:output overWrite:YES] != NO )
{
NSLog(@"Unziped %@ ",[zippedFiles objectAtIndex:i]);
}

[za UnzipCloseFile];
[self deleteFile:[zippedFiles objectAtIndex:i]];
}
else
{
NSLog(@"The file %@ not exist",[zippedFiles objectAtIndex:i]);

}
doneBtn.hidden = NO;
[lb7 setText:@""];


}
}

最佳答案

在后台任务中解压缩并将 UI 更新分派(dispatch)到主线程:

-(void) UnzipFiles
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
for (int i=0; i < zippedFiles.count; i++) {

dispatch_async(dispatch_get_main_queue(), ^{
lb7.text = …;
});
}
});
}

关于ios - setText 不适用于 UI 更新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17637504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com