__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance

4.3k views Asked by At

I'm trying to calculate duration difference between two times. I get the durations from JSONArray. I use the below code

NSDate *starttime = [[NSDate alloc]init];
NSDate *endtime = [[NSDate alloc]init];
starttime = [currentRecord valueForKey:@"starttime"];
endtime = [currentRecord valueForKey:@"endtime"];

I pass the above values to another method

+(NSString*)remaningTime:(NSDate*)startDate endDate:(NSDate*)endDate
{
 NSDateComponents *components= [[NSDateComponents alloc]init];
 NSString *durationString;

 components = [[NSCalendar currentCalendar] components:       NSCalendarUnitDay/*|NSCalendarUnitHour|NSCalendarUnitMinute*/ fromDate: endDate toDate: startDate options: 0];
}

but I get below exception

-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x78685f80

2016-12-20 15:21:37.361 nexge[1515:183039] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x78685f80'
*** First throw call stack:
(
    0   CoreFoundation                      0x03abc212 __exceptionPreprocess + 194
    1   libobjc.A.dylib                     0x041d3e66 objc_exception_throw + 52
    2   CoreFoundation                      0x03b353dc -[NSObject(NSObject) doesNotRecognizeSelector:] + 172
    3   CoreFoundation                      0x03a3b34c ___forwarding___ + 1052
    4   CoreFoundation                      0x03a3af0e _CF_forwarding_prep_0 + 14
    5   CoreFoundation                      0x03a9b419 -[__NSCFCalendar components:fromDate:toDate:options:] + 393
    6   CoreFoundation                      0x03a9b217 -[_NSCopyOnWriteCalendarWrapper components:fromDate:toDate:options:] + 103
    7   nexge                               0x0003c2c8 +[callRecords remaningTime:endDate:] + 248
    8   nexge                               0x0003c0f9 -[callRecords initWithCallAmount:duration:start:end:destination:] + 393
    9   nexge                               0x00036321 -[callRecordsDataController addRecordWithCallAmount:duration:start:end:destination:] + 257
    10  nexge                               0x00036d0d -[callRecordsDataController connectionDidFinishLoading:] + 1165
    11  CFNetwork                           0x00464850 ___ZL32_NSURLConnectionDidFinishLoadingP16_CFURLConnectionPKv_block_invoke + 55
    12  CFNetwork                           0x004603b8 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 71
    13  CFNetwork                           0x00460369 -[NSURLConnectionInternalConnection invokeForDelegate:] + 142
    14  CFNetwork                           0x004602c7 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 188
    15  CFNetwork                           0x004601f7 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 42
    16  CFNetwork                           0x00464814 _ZL32_NSURLConnectionDidFinishLoadingP16_CFURLConnectionPKv + 36
    17  CFNetwork                           0x00464769 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 96
    18  CFNetwork                           0x00685f81 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 107
    19  libdispatch.dylib                   0x04cb0cc3 _dispatch_client_callout + 14
    20  libdispatch.dylib                   0x04c895f2 _dispatch_block_invoke_direct + 380
    21  libdispatch.dylib                   0x04c9497f ___dispatch_block_create_block_invoke + 20
    22  CFNetwork                           0x004600ce _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 24
    23  CoreFoundation                      0x03a06ef9 CFArrayApplyFunction + 57
    24  CFNetwork                           0x0045ffc9 _ZN19RunloopBlockContext7performEv + 139
    25  CFNetwork                           0x005664a8 _ZThn16_N19RunloopBlockContext24multiplexerClientPerformEv + 20
    26  CFNetwork                           0x0045fe65 _ZN17MultiplexerSource7performEv + 319
    27  CFNetwork                           0x0045fc84 _ZN17MultiplexerSource8_performEPv + 62
    28  CoreFoundation                      0x03a5bcbf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    29  CoreFoundation                      0x03a3fdf7 __CFRunLoopDoSources0 + 519
    30  CoreFoundation                      0x03a3f284 __CFRunLoopRun + 1124
    31  CoreFoundation                      0x03a3ebab CFRunLoopRunSpecific + 395
    32  CoreFoundation                      0x03a3ea0b CFRunLoopRunInMode + 123
    33  GraphicsServices                    0x0575eb4c GSEventRunModal + 177
    34  GraphicsServices                    0x0575e9c7 GSEventRun + 80
    35  UIKit                               0x0225c32b UIApplicationMain + 148
    36  nexge                               0x0003702c main + 140
    37  libdyld.dylib                       0x04ceb799 start + 1
    38  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
2

There are 2 answers

0
Devang Tandel On BEST ANSWER

You are sending String data where NSDate is required. type mismatch is leading to crash your application. You must convert your string into Date before using your function, Here is how you can convert.

NSString *dateStr = @"2016/12/20 12:53:58 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy/MM/dd HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];

Note: yyyy/MM/dd HH:mm:ss Z is the date format i have used to show example, you have change this format as per your string.

//date is what you need to pass,

You have to do this for both your date,

0
amit_donga On

Try this one:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSString *startStr = @"2016-10-05 10:20:00 +0000";
NSString *endStr = @"2016-10-05 10:25:00 +0000";
NSDate *startTime = [dateFormatter dateFromString:startStr]; //[currentRecord valueForKey:@"starttime"];
NSDate *endTime = [dateFormatter dateFromString:endStr]; //[currentRecord valueForKey:@"endtime"];

NSTimeInterval secondsBetween = [endTime timeIntervalSinceDate:startTime];
NSLog(@"deference :%f",secondsBetween);