美文网首页
iOS项目开发中Crash汇总

iOS项目开发中Crash汇总

作者: 480a52903ce5 | 来源:发表于2017-09-07 07:39 被阅读68次

错误1、


图1

原因: 从上面的报错信息可以看出,主线程在运行的时候子线程修改了主线程UI的布局约束,在iOS开发中,所有的有关界面UI的更新操作必须奥在主线程中完成。这样的错误很容易出现在使用block的时候,因为block就是在子线程中进行的,所以回顾了刚才自己写的代码,发现还真是粗心了。解决的办法就是在刚才写的代码中有关针对UI更新的操作放到主线程中。

错误2、

Warning: Attempt to present <UIAlertController: 0x7fd2f8609240> on <ViewController: 0x7fd2f85090b0> whose view is not in the window hierarchy!

// 原因: 今天在写UIAlertController的时候,在ViewDidLoad中声明并模态推出的时候出现了一个错误,这个错误网上查了一下,原因是在presnet的时候viewDidLoad还没有执行完成,只有viewDidLoad执行完成之后,正常使用。
在控制器加载的时候,不能使用这个去调用,这样就必须向办法延时才行。
由于是Demo,习惯性的直接写在viewDidLoad中,我最后使用一个Button来出发这个动作,就没有以上错误了。

错误3

Assertion failure in -[UITableView _dequeueReusableViewOfType:withIdentifier:]
// 可能是XIB中多脱出一个控件导致

错误4

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x1700063f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
可能是xib的某个控件没有对应的关联上

错误5

Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Cache
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    这个函数的返回值是个null!!
    查stackoverflow 找到下面的解。
CellIdentifier I bet your cellForRowAtIndexPath is returning null.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"Photos";

    /** NOTE: This method can return nil so you need to account for that in code */
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // NOTE: Add some code like this to create a new cell if there are none to reuse
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    NSString *string = [[self.photosInPlace objectAtIndex:indexPath.row]     valueForKeyPath:@"description._content"];

    cell.textLabel.text = string;

    return cell;
}
That's probably why [UITableView _configureCellForDisplay:forIndexPath:] is failing... becausecellForRowAtIndexPath is returning a null value and then configureCellForDisplay is expecting aUITableViewCell.

通过解决这个错误,认识到一点:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中一定要注意避免所返回的cell为nil的情况出现。实际上,从iOS5开始,一个表视图可以跟踪与特定的可重用标识符相关联的nib文件。UITableView的
dequeueReusableCellWithIdentifier:方法现在很智能,就算没有可用的单元,它也可以使用这个注册了的nib文件来加载一个新单元。这就意味着,只要我们为表视图注册了所有将要使用到的可重用标识符,dequeueReusableCellWithIdentifier:方法就会始终返回一个单元,它决不会返回nil。因此,我们可以删除那些用于检查cell为nil的代码行,因为这种情况永远不会发生。
所以,这里更好的做法是用如下方法来实现-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    static NSString *CellTableIdentifer = @"CellTableIdentifer";  
    //tableView注册nib文件  
    static BOOL nibsRegistered = NO;  
    if(!nibsRegistered)  
    {  
        UINib *nib = [UINib nibWithNibName:@"TableViewItemCell" bundle:nil];  
        [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifer];  
        nibsRegistered = YES;  
    }  
      
    TableViewItemCell * cell = [tabView dequeueReusableCellWithIdentifier:CellTableIdentifer];  
    cell.textLabel.text = @"xxxxx";  
    return cell;  
}  

错误6

Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x11be33cc0) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x11ac826f0). One of the two will be used. Which one is undefined.

屏蔽杂乱无章的log,在xcode Edit scheme -> Run -> Arguments-> Environment Variables->Name:OS_ACTIVITY_MODE Value:disable

错误7

This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

权限以及相关设置出问题:

<!-- 相册 --> 
<key>NSPhotoLibraryUsageDescription</key> 
<string>App需要您的同意,才能访问相册</string> 
<!-- 相机 --> 
<key>NSCameraUsageDescription</key> 
<string>App需要您的同意,才能访问相机</string> 
<!-- 麦克风 --> 
<key>NSMicrophoneUsageDescription</key> 
<string>App需要您的同意,才能访问麦克风</string> 
<!-- 位置 --> 
<key>NSLocationUsageDescription</key> 
<string>App需要您的同意,才能访问位置</string> 
<!-- 在使用期间访问位置 --> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>App需要您的同意,才能在使用期间访问位置</string> 
<!-- 始终访问位置 --> 
<key>NSLocationAlwaysUsageDescription</key> 
<string>App需要您的同意,才能始终访问位置</string> 
<!-- 日历 --> 
<key>NSCalendarsUsageDescription</key> 
<string>App需要您的同意,才能访问日历</string> 
<!-- 提醒事项 --> 
<key>NSRemindersUsageDescription</key> 
<string>App需要您的同意,才能访问提醒事项</string> 
<!-- 运动与健身 --> 
<key>NSMotionUsageDescription</key> <string>App需要您的同意,才能访问运动与健身</string> 
<!-- 健康更新 --> 
<key>NSHealthUpdateUsageDescription</key> 
<string>App需要您的同意,才能访问健康更新 </string> 
<!-- 健康分享 --> 
<key>NSHealthShareUsageDescription</key> 
<string>App需要您的同意,才能访问健康分享</string> 
<!-- 蓝牙 --> 
<key>NSBluetoothPeripheralUsageDescription</key> 
<string>App需要您的同意,才能访问蓝牙</string> 
<!-- 媒体资料库 --> 
<key>NSAppleMusicUsageDescription</key> 
<string>App需要您的同意,才能访问媒体资料库</string>

如果不起作用,可以请求后台权限,类似于这样:

<key>UIBackgroundModes</key>
<array> 
<!-- 在这里写上你在后台模式下要使用权限对应的key --> 
<string>location</string>
...
</array>

/**  Plist 字段*/
麦克风权限:Privacy - Microphone Usage Description
通讯录权限: Privacy - Contacts Usage Description
蓝牙权限:Privacy - Bluetooth Peripheral Usage Description
语音转文字权限:Privacy - Speech Recognition Usage Description
日历权限:Privacy - Calendars Usage Description
定位权限:Privacy - Location When In Use Usage Description
定位权限: Privacy - Location Always Usage Description

待续……

相关文章

网友评论

      本文标题:iOS项目开发中Crash汇总

      本文链接:https://www.haomeiwen.com/subject/ezfedxtx.html