当我在搜索栏中输入时,我的应用程序崩溃了

Posted

技术标签:

【中文标题】当我在搜索栏中输入时,我的应用程序崩溃了【英文标题】:when i type in search bar my app crashes 【发布时间】:2016-12-15 07:24:50 【问题描述】:

这是我的 .h 文件。在这个文件中,我声明了搜索结果的数组,我想用于过滤器的邮件数组是 arrfullname。

DisplayAppointmentViewController.h

//
//  DisplayAppointmentViewController.h
//  AffordPlan
//
//  Created by apple on 17/08/16.
//  Copyright © 2016 Taxsmart. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "CustomClass.h"
#import "AppoinmentTableViewCell.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import "AddAppoinmentViewController.h"
#import "CreatePlanViewController.h"
#import "LeadViewController.h"
#import "Sqlite.h"
#import "outComeViewController.h"

@class RadioButton;


@interface DisplayAppointmentViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate,UISearchControllerDelegate,UISearchBarDelegate,UISearchResultsUpdating,MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate>





    NSArray *arrsearchresult;


    NSMutableArray *arrfullname;
    NSMutableArray *arrdatetime;

    NSMutableArray *arrappointment_date;
    NSMutableArray *appointmenttime;





@property (strong, nonatomic) IBOutlet UITableView *tblAppoinment;
@property (strong, nonatomic) IBOutlet RadioButton *btnSearchByName;
@property (strong, nonatomic) IBOutlet RadioButton *btnSearchByDate;
@property (strong, nonatomic) IBOutlet UISearchBar *searchResultBar;

- (IBAction)btnSearchByNamePress:(id)sender;
- (IBAction)btnSearchByDatePress:(id)sender;

@property CustomClass *customClass;

- (IBAction)btnBackPressed:(id)sender;


@end

这是我的 .m 文件 DisplayAppointmentViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) 
        return [arrsearchresult count];

     else 
        return [arrfullname count];
    


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    AppoinmentTableViewCell *cell = (AppoinmentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AppoinmentTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        if (tableView == self.searchDisplayController.searchResultsTableView)
        
            arrfullname = [arrsearchresult objectAtIndex:indexPath.row];

            cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
            cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
            cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];


        


        cell.btnCall.tag = indexPath.row;
        cell.btnMail.tag = indexPath.row;
        cell.btnSms.tag = indexPath.row;
        cell.btnCreatePlan.tag = indexPath.row;
        cell.btnAppointment.tag = indexPath.row;
        cell.btnStatus.tag=indexPath.row;

    

    cell.clipsToBounds = YES;

    if ([strCheckVC isEqualToString:@"OpenLead"])
    
        cell.lblFullname.text = [arrfullname objectAtIndex:indexPath.row];

        [self databaseOpen];

        NSString *query_user=[ NSString stringWithFormat:@"Select * from lead_status where row_lead_id = %@ and lead_followup_datetime != ' ' order by rowid desc limit 1",[arrRowid objectAtIndex:indexPath.row]];

        NSArray *rowids = [[NSArray alloc]init];
        rowids =[[database executeQuery:query_user]mutableCopy];

        NSLog(@"%@",rowids);

        [database close];

        if (rowids.count > 0 )
        
            NSString *dateString = [[rowids valueForKey:@"lead_followup_datetime"]objectAtIndex:0];

            NSArray *components = [dateString componentsSeparatedByString:@" "];

            NSString *date = components[0];
            NSString *time = components[1];

            // date convert
            NSString *dateStr = [NSString stringWithFormat:@"%@",date];

            NSString *timestr = [NSString stringWithFormat:@"%@",time];


            // Convert string to date object
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"HH:mm:ss"];
            NSDate *date11 = [dateFormat dateFromString:timestr];



            NSDateFormatter* df = [[NSDateFormatter alloc]init];
            [df setDateFormat:@"hh:mm a"];
            NSString *depResult = [df stringFromDate:date11];

            cell.lblTime.text = depResult;

            dateStr = [NSString stringWithFormat:@"%@",date];

            // Convert string to date object
            dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"yyyy-mm-dd"];
            NSDate *viewdt = [dateFormat dateFromString:dateStr];
            //date11 = [dateFormat dateFromString:dateStr];
            NSLog(@"%@",date);



            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSDate *date22 = [dateFormatter dateFromString:dateStr];

            [dateFormatter setDateFormat:@"dd MMM yyyy"];
            NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
            NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);


            //            df = [[NSDateFormatter alloc]init];
            //            [df setDateFormat:@"dd MMM yyyy"];
            //            depResult = [df stringFromDate:viewdt];
            //
            cell.lblDate.text = dateWithNewFormat;



        
        else
        
            // date convert
            NSString *dateStr = [NSString stringWithFormat:@"%@",[appointmenttime objectAtIndex:indexPath.row]];

            // Convert string to date object
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"HH:mm:ss"];
            NSDate *date = [dateFormat dateFromString:dateStr];
            NSLog(@"%@",date);

            NSDateFormatter* df = [[NSDateFormatter alloc]init];
            [df setDateFormat:@"hh:mm a"];
            NSString *depResult = [df stringFromDate:date];

            cell.lblTime.text = depResult;


            dateStr = [NSString stringWithFormat:@"%@",[arrappointment_date objectAtIndex:indexPath.row]];

            // Convert string to date object
            dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"yyyy-mm-dd"];
            date = [dateFormat dateFromString:dateStr];
            NSLog(@"%@",date);




            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSDate *date22 = [dateFormatter dateFromString:dateStr];

            [dateFormatter setDateFormat:@"dd MMM yyyy"];
            NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
            NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);


            //            df = [[NSDateFormatter alloc]init];
            //            [df setDateFormat:@"dd MMM yyyy"];
            //            depResult = [df stringFromDate:date];
            //
            cell.lblDate.text = dateWithNewFormat;



        


        cell.selectionStyle = UITableViewCellSelectionStyleNone;


        if ([[arremail objectAtIndex:indexPath.row] isEqual:[NSNull null]] || [[arremail objectAtIndex:indexPath.row] isEqualToString:@"<null>"] || [[arremail objectAtIndex:indexPath.row] isEqualToString:@""] )
        

            cell.btnMail.hidden = NO;
        


        NSUInteger hours_passed = [[arrhours_passed objectAtIndex:indexPath.row] integerValue];

        if (hours_passed <= 72)
        

            cell.colorView.backgroundColor = [UIColor whiteColor];

        

        else if (hours_passed <= 144)
        
            cell.colorView.backgroundColor = [UIColor colorWithRed:219.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];

            cell.lblDate.textColor = [UIColor whiteColor];
            cell.lblTime.textColor = [UIColor whiteColor];
            cell.lblFullname.textColor = [UIColor whiteColor];


        
        else if (hours_passed <= 216)
        
            cell.colorView.backgroundColor = [UIColor colorWithRed:202.0/255.0 green:114.0/255.0 blue:114.0/255.0 alpha:1.0];
            cell.lblDate.textColor = [UIColor whiteColor];
            cell.lblTime.textColor = [UIColor whiteColor];
            cell.lblFullname.textColor = [UIColor whiteColor];
        
        else if (hours_passed <= 288)
        
            cell.colorView.backgroundColor = [UIColor colorWithRed:189.0/255.0 green:78.0/255.0 blue:78.0/255.0 alpha:1.0];
            cell.lblDate.textColor = [UIColor whiteColor];
            cell.lblTime.textColor = [UIColor whiteColor];
            cell.lblFullname.textColor = [UIColor whiteColor];
        



        [self databaseOpen];


        NSString *str = [NSString stringWithFormat:@"Select * from lead_followup where rowid=%@",[arrRowid objectAtIndex:indexPath.row]];


        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [[database executeQuery:str]mutableCopy];
        NSLog(@"arr data %@",arr);


        [database close];

        NSString *strsms;




        if (arr.count != 0)
        
            strsms =  [NSString stringWithFormat:@"%@",[[arr valueForKey:@"followed_by_sms"]objectAtIndex:0]];

            if ([strsms isEqualToString:@"0"])
            
                if (hours_passed <= 72)
                
                    [cell.btnSms setImage:[UIImage imageNamed:@"Chatgray"] forState:UIControlStateNormal];

                
                else if (hours_passed > 72)
                
                    //white

                    [cell.btnSms setImage:[UIImage imageNamed:@"chatwhite"] forState:UIControlStateNormal];

                
            
            /*else
             
             // green icon
             [cell.btnSms setImage:[UIImage imageNamed:@"Chatgreen"] forState:UIControlStateNormal];
             */


            NSString *strCall =  [NSString stringWithFormat:@"%@",[[arr valueForKey:@"followed_by_call"]objectAtIndex:0]];
            NSLog(@"%@",strCall);
            if ([strCall isEqualToString:@"0"])
            
                if (hours_passed <= 72)
                
                    //blackCallGray

                    [cell.btnCall setImage:[UIImage imageNamed:@"CallGray"] forState:UIControlStateNormal];


                
                else if (hours_passed > 72)
                
                    [cell.btnCall setImage:[UIImage imageNamed:@"CallWhite"] forState:UIControlStateNormal];

                
            
            else
            
                [cell.btnCall setImage:[UIImage imageNamed:@"CallGreen"] forState:UIControlStateNormal];
                // green icon

            



            NSString *strmail =  [NSString stringWithFormat:@"%@",[[arr valueForKey:@"followed_by_mail"]objectAtIndex:0]];
            NSLog(@"%@",strmail);

            if ([strmail isEqualToString:@"0"])
            

                if (hours_passed <= 72)
                

                    [cell.btnMail setImage:[UIImage imageNamed:@"Mailgray"] forState:UIControlStateNormal];

                
                else if (hours_passed > 72)
                
                    [cell.btnMail setImage:[UIImage imageNamed:@"MailWhite"] forState:UIControlStateNormal];
                    //white

                
            
            /*else
             
             [cell.btnMail setImage:[UIImage imageNamed:@"MailGreen"] forState:UIControlStateNormal];
             // green icon


             */


        


        [cell.btnCall addTarget:self
                         action:@selector(callDo:) forControlEvents:UIControlEventTouchDown];

        [cell.btnSms addTarget:self
                        action:@selector(smsDo:) forControlEvents:UIControlEventTouchDown];
        [cell.btnMail addTarget:self
                         action:@selector(mailDo:) forControlEvents:UIControlEventTouchDown];


        [cell.btnStatus addTarget:self
                           action:@selector(Status:) forControlEvents:UIControlEventTouchDown];

        [cell.btnAppointment addTarget:self
                                action:@selector(AddappointmentId:) forControlEvents:UIControlEventTouchDown];

        [cell.btnCreatePlan addTarget:self
                               action:@selector(CreatePlan:) forControlEvents:UIControlEventTouchDown];



    
    else
    
        cell.lblFullname.text = [arrfullname objectAtIndex:indexPath.row];
        cell.btnSms.hidden = YES;
        cell.btnCall.hidden = YES;
        cell.btnMail.hidden = YES;
        cell.colorView.hidden  =YES;



        //  cell.lblTime.text = [appointmenttime objectAtIndex:indexPath.row];
        //cell.lblDate.text = [arrappointment_date objectAtIndex:indexPath.row];



        NSString *dateStr = [NSString stringWithFormat:@"%@",[appointmenttime objectAtIndex:indexPath.row]];

        // Convert string to date object
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"HH:mm:ss"];
        NSDate *date = [dateFormat dateFromString:dateStr];
        NSLog(@"%@",date);

        NSDateFormatter* df = [[NSDateFormatter alloc]init];
        [df setDateFormat:@"hh:mm a"];
        NSString *depResult = [df stringFromDate:date];

        cell.lblTime.text = depResult;



        dateStr = [NSString stringWithFormat:@"%@",[arrappointment_date objectAtIndex:indexPath.row]];

        // Convert string to date object
        dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-mm-dd"];
        date = [dateFormat dateFromString:dateStr];
        NSLog(@"%@",date);



        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *date22 = [dateFormatter dateFromString:dateStr];

        [dateFormatter setDateFormat:@"dd MMM yyyy"];
        NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
        NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);

        //        df = [[NSDateFormatter alloc]init];
        //        [df setDateFormat:@"dd MMM yyyy"];
        //        depResult = [df stringFromDate:date];
        //
        cell.lblDate.text = dateWithNewFormat;


    



    /*  UIView *bgColorView = [[UIView alloc] init];
     bgColorView.backgroundColor = [UIColor clearColor];
     [cell setSelectedBackgroundView:bgColorView];*/


    return cell;



- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    arrsearchresult = [arrfullname filteredArrayUsingPredicate:resultPredicate];
    [self.tblAppoinment reloadData];


-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString

    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;

我的错误日志是

2016-12-15 15:07:44.534 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x7fb59aef6930'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010898ae65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108403deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010899348d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001088e090a ___forwarding___ + 970
    4   CoreFoundation                      0x00000001088e04b8 _CF_forwarding_prep_0 + 120
    5   xxxxx                          0x0000000107d629ee -[DisplayAppointmentViewController tableView:cellForRowAtIndexPath:] + 10894
    6   UIKit                               0x0000000109910e43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766
    7   UIKit                               0x0000000109910f7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
    8   UIKit                               0x00000001098e5a39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996
    9   UIKit                               0x000000010991a01c -[UITableView _performWithCachedTraitCollection:] + 92
    10  UIKit                               0x0000000109900edc -[UITableView layoutSubviews] + 224
    11  UIKit                               0x000000010986e4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
    12  QuartzCore                          0x00000001095fa59a -[CALayer layoutSublayers] + 146
    13  QuartzCore                          0x00000001095eee70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    14  QuartzCore                          0x00000001095eecee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    15  QuartzCore                          0x00000001095e3475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    16  QuartzCore                          0x0000000109610c0a _ZN2CA11Transaction6commitEv + 486
    17  UIKit                               0x00000001097b1f7c _UIApplicationHandleEventQueue + 7329
    18  CoreFoundation                      0x00000001088b6a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    19  CoreFoundation                      0x00000001088ac95c __CFRunLoopDoSources0 + 556
    20  CoreFoundation                      0x00000001088abe13 __CFRunLoopRun + 867
    21  CoreFoundation                      0x00000001088ab828 CFRunLoopRunSpecific + 488
    22  GraphicsServices                    0x000000010ed40ad2 GSEventRunModal + 161
    23  UIKit                               0x00000001097b7610 UIApplicationMain + 171
    24  xxxxx                          0x0000000107d50bbf main + 111
    25  libdyld.dylib                       0x000000010adfa92d start + 1
    26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

什么是崩溃日志? 2016-12-15 13:02:10.836 AffordPlan[2884:91271] *** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ valueForUndefinedKey:]: this类与键名的键值编码不兼容。 *** 首先抛出调用栈: 你的谓词中的name是什么? arrfullname 包含什么?显示此数组中对象的结构。 【参考方案1】:
  Foundation                          0x000000010e034888 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
   Foundation                          0x000000010df8a997 -[NSObject(NSKeyValueCoding) valueForKey:] + 280
   Foundation                          0x000000010dfc5319 -[NSFunctionExpression expressionValueWithObject:context:] + 1096

您的崩溃日志说方法中谓词的键值存在问题

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

放置断点,检查代码运行时间并根据数据源更正谓词。

【讨论】:

我的应用程序在 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope method : arrsearchresult = [arrfullname filteredArrayUsingPredicate:resultPredicate]; 我更改了谓词的键值。现在我得到了这个错误日志。

以上是关于当我在搜索栏中输入时,我的应用程序崩溃了的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用空输入字段提交时,React 应用程序崩溃

我的 C 程序不断崩溃

按活动栏中的按钮导致应用程序崩溃

当我在 GSON(使用枚举)上运行 Proguard 时应用程序崩溃

来自 AppDelegate 的当前 viewController 崩溃/空屏

当我尝试使用 ArrayAdapter 时,我的应用程序崩溃了