ios 開發,常見報錯及修復

錯誤一:

編譯會報錯: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 文件


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章