ios 开发,常见报错及修复

这篇博客列举了iOS开发中遇到的五个常见错误,包括ARC模式下结构体中的OC对象报错、全局变量在异步操作中不可赋值、在后台线程修改UI警告。并提供了详细的修复方法,如使用`__unsafe_unretained`关键字、添加`__block`修饰符、通过GCD在主线程更新UI等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误一:

编译会报错:ARC forbids Objective-C object in struct     ARC模式下,不允许将OC对象定义在结构体中

修正: 定义语句前加  __unsafe_unretained


错误二:

编译报错: Unable to access global variables in dispatch_async : “Variable is not Assignable (missing _block type specifier)” [duplicate]

修正:语句前加 __block


错误三:

运行警告:在后台非主线程中刷新UI:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes


修正:方法一:通过GCD,在主线程执行代码

dispatch_async(dispatch_get_main_queue(), ^{

        。。。

    });

方法二:通过通知,但是通知的注册应该在主线程,如下需要用到queue参数,并且传mainQueue参数。

addObserverForName:@"notification"
    object:nil
    queue:[NSOperationQueue mainQueue]
    usingBlock:^(NSNotification *note){
        // Do UI stuff here
    }
方法三:在主线程发起通知

[self performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:NO];

错误四:

在ipad上实现AlertController时,运行报错

Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x181b6400>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem.  If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

解决方案,来自stackoverflow:

You can present a UIAlertController from a popover by using UIPopoverPresentationController.

UIViewController *self; // code assumes you're in a view controller
UIButton *button; // the button you want to show the popup sheet from

UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;

alertController = [UIAlertController alertControllerWithTitle:nil
                                                      message:nil
                           preferredStyle:UIAlertControllerStyleActionSheet];
destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
                                         style:UIAlertActionStyleDestructive
                                       handler:^(UIAlertAction *action) {
                                           // do destructive stuff here
                                       }];
otherAction = [UIAlertAction actionWithTitle:@"Blah"
                                       style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction *action) {
                                         // do something here
                                     }];
// note: you can control the order buttons are shown, unlike UIActionSheet
[alertController addAction:destroyAction];
[alertController addAction:otherAction];
[alertController setModalPresentationStyle:UIModalPresentationPopover];

UIPopoverPresentationController *popPresenter = [alertController 
                                              popoverPresentationController];
popPresenter.sourceView = button;
popPresenter.sourceRect = button.bounds;
[self presentViewController:alertController animated:YES completion:nil];
起关键作用的代码用红色标记。

错误五:

在通过[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];删除cell时,总是崩溃:
网友大多建议尝试

[tableView beginUpdates];

[tableView endUpdates];

    [tableView reloadData];

或者重新创建indexPath等等。但是都不适合我这里的情况,折磨我够长的时间,最后在stackoverflow上偶尔看到,

可能是你没有删除对应model中的数据,果然,我在添加对模型数据的删除后,成功执行了。

问题:在开发中,引入了zip库,出现下面的错误

 
Undefined symbols for architecture i386:
  "_compressBound", referenced from:
      +[NSDataGZipAdditions compressedDataWithBytes:length:] in NSDataGZipAdditions.o
  "_compress", referenced from:
      +[NSDataGZipAdditions compressedDataWithBytes:length:] in NSDataGZipAdditions.o
  "_inflateInit_", referenced from:
      +[NSDataGZipAdditions dataWithCompressedBytes:length:] in NSDataGZipAdditions.o
  "_inflate", referenced from:
      +[NSDataGZipAdditions dataWithCompressedBytes:length:] in NSDataGZipAdditions.o
      -[ParserForMainReturn uncompressZippedData:] in ParserForMainReturn.o
  "_inflateEnd", referenced from:
      +[NSDataGZipAdditions dataWithCompressedBytes:length:] in NSDataGZipAdditions.o
      -[ParserForMainReturn uncompressZippedData:] in ParserForMainReturn.o
  "_inflateInit2_", referenced from:
      -[ParserForMainReturn uncompressZippedData:] in ParserForMainReturn.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
复制代码

如果老是出现上面的错误。那么在工程中加入libz.dlib 文件


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值