项目中遇到的一些问题小结

1,081 阅读3分钟

最近项目中遇到两个关于iOS8 系统下项目crash的问题,刚好项目做的差不多了,就写下来做个记录,以后方便查看

****1、项目中用到系统的左滑删除功能,正常情况下左滑删除并没什么毛病,可怕的地方就在于测试小姐姐在左滑后并未删除,而是直接点了屏幕左上角的返回按钮

这下事大了,直接了当的crash掉,而且并未能看到任何有营养的日志。

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式,按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        BaseConfig *aConfig = self.dataArray[indexPath.row];
        [self.dataArray removeObjectAtIndex:indexPath.row];
        SQLDataManager *manager = [SQLDataManager sharedDataManager];
        [manager deleateSingleData:[aConfig.sTime substringWithRange:NSMakeRange(0, 4)] sID:aConfig.sID];
        [self.tableview reloadData];
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

强大的main函数,你好,又见面了

Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)

习惯性打开全局断点,打开僵尸对象的开关,重复进行小姐姐的操作,终于看到了崩溃日志,终于是看到了些

2018-06-01 16:48:45.534 WuYouQianBao[58470:4515935] *** -[QBSearchSuggestionViewController tableView:canEditRowAtIndexPath:]: message sent to deallocated instance 0x18837dc0

看到这个,我开始以为是这个类写的有内存泄漏,于是乎重写dealloc方法,打印了一波

2018-06-01 16:48:45.518 [58470:4515935] 1111111---> dealloc
2018-06-01 16:48:45.520 [58470:4515935] 1111111---> dealloc

看到这两行日志时候,都想骂娘了,什么鬼?

作为一名优秀的“百度”程序员,怎么会不问问度娘呢?于是乎打开百度...

终于知道原因了,网上有大神说可能是苹果的bug?终于可以摔锅了~偷笑😏

这里也有一个他山之石

原因知道了,锅也甩了,然后解bug

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.tableview setEditing:NO];
}


在离开这个页面时候设置下编辑状态未不可编辑,完美解决~

****2、关于UITextView的一个bug,由于UITextView木有placeholder,而我们现在的设计总是会加上placeholder,此时就出现了两条路 --->自己想办法加个placeholder在系统UITextView上面

我们项目里面写了个categroy 用了强大的runtime给系统textView添加了一个placeHolder,然后就是这个categroy的锅,导致在iOS8上面会产生crash,话说是在dealloc的时候removeObserver时候,可能有内存泄漏(据说是酱紫,而且只是掉remove的代码就能好)。就算是酱紫吧,注释掉这个,或者iOS8 不做处理~

--->网上找找第三方常用的解决方案(YYTextView 就有placeholder) 由于我司项目是用的cocopod把category中封装在私有库中,负责私有库的小哥哥觉得注释掉remove不合适,心有不甘,而crash还依然缠绕着我,没办法只能

#import <UIKit/UIKit.h>

@interface UITextView (HJPlaceHolder)

/**
 给 UITextView 添加的 placeHolderView 可以用来配置样式
 */
@property (nonatomic, strong) UITextView *hj_placeHolderTextView;

/**
 通过 String 给 UITextView 添加 placeHolder

 @param placeHolder placeHolder 上显示的 string
 */
- (void)hj_addPlaceHolder:(NSString *)placeHolder;

@end
#import "UITextView+HJPlaceHolder.h"

#import <objc/runtime.h>

static const char *hj_placeHolderTextView = "hj_placeHolderTextView";

@implementation UITextView (HJPlaceHolder)

- (UITextView *)hj_placeHolderTextView {
    return objc_getAssociatedObject(self,hj_placeHolderTextView);
}
- (void)setHj_placeHolderTextView:(UITextView *)placeHolderTextView {
    objc_setAssociatedObject(self, hj_placeHolderTextView, placeHolderTextView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)hj_addPlaceHolder:(NSString *)placeHolder {
    if (![self hj_placeHolderTextView]) {
        UITextView *textView = [[UITextView alloc] initWithFrame:self.bounds];
        textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        textView.font = self.font;
        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = [UIColor grayColor];
        textView.userInteractionEnabled = NO;
        textView.text = placeHolder;
        [self addSubview:textView];
        [self setHj_placeHolderTextView:textView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];
        
    }
    self.hj_placeHolderTextView.text = placeHolder;
}
# pragma mark -
# pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(NSNotification *)noti {
    self.hj_placeHolderTextView.hidden = YES;
}
- (void)textViewDidEndEditing:(UITextView *)noti {
    if (self.text && [self.text isEqualToString:@""]) {
        self.hj_placeHolderTextView.hidden = NO;
    }
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)
2018-06-01 17:13:53.570 WuYouQianBao[58551:4520883] *** -[UITextView textInputView]: message sent to deallocated instance 0x1638e800

网上也有人讲了