Recently, I encountered two problems about the crash of the project in iOS8 system. Just when the project was almost finished, I wrote it down and made a record for convenient checking in the future

****1. The system’s left-swipe deletion function is used in the project. Under normal circumstances, there is nothing wrong with left-swipe deletion

This is a big deal, direct crash, and did not see any nutritional log.

/ / define editing style - (UITableViewCellEditingStyle) tableView: (UITableView *) tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath {returnUITableViewCellEditingStyleDelete; } // Enter edit mode, - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(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]; }} // Modify edit button text - (NSString *)tableView (UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {return @"Delete";
}
Copy the code

The powerful main function. Hello again

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

Copy the code

The 2018-06-01 16:48:45. 534 WuYouQianBao [58470-4515935] * * * - [QBSearchSuggestionViewController tableView:canEditRowAtIndexPath:]: message sent to deallocated instance 0x18837dc0Copy the code

Seeing this, I initially assumed that the class was writing a memory leak, so I overwrote the dealloc method and printed a wave

2018-06-01 16:48:45.518 [5847:4515935] 2018-06-01 16:48:45.520 [5847:4515935] 2018-06-01 16:48:45.520 [5847:4515935] 2018-06-01 16:48:45.520 [5847:4515935] 2018-06-01 16:48:45.520 [5847:4515935] 2018-06-01 16:48:45.520 [5847:4515935Copy the code

When I see these two lines, I want to swear. What the hell?

As a good “Baidu” programmer, why not ask Baidu? So open Baidu…

Finally know the reason, the Internet god said that maybe apple bug? Finally can throw the pot ~ steal smile 😏

Here, too, is a gem

We know why, we dump the pot, and we fix the bug

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


Copy the code

When leaving this page, set the edit status to be not uneditable, perfect solution ~

****2, a bug about UITextView, because UITextView does not have placeholder, and our current design always adds placeholder, There are two ways to put a placeholder on the UITextView

So we have a categroy in our project and we’ve added a placeHolder to the textView system with powerful runtime, and then we have this categroy pot, which is causing crashes in iOS8, – There may be a memory leak in dealloc when removeObserver is used. Comment this out, or iOS8 doesn’t handle it

#import <UIKit/UIKit.h>@interface UITextView (HJPlaceHolder) /** @property (nonatomic, strong) UITextView *hj_placeHolderTextView; /** Add placeHolder @param placeHolder placeHolder */ - to the UITextView view (void)hj_addPlaceHolder:(NSString *)placeHolder; @endCopy the code
#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]; [selfsetHj_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
Copy the code
Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)
Copy the code
2018-06-01 17:13:533. 557 WuYouQianBao[58551:4520883] *** -[UITextView textInputView]: message sent to deallocated instance 0x1638e800Copy the code

There were people on the Internet