__NSCFStringtimeIntervalSinceReferenceDate]:无法识别的选择器发送到实例

__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance

我正在尝试计算两次之间的持续时间差异。我从 JSONArray 获取持续时间。我使用下面的代码

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

我将上述值传递给另一个方法

+(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];
}

但我得到以下异常

-[__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

您正在向需要 NSDate 的地方发送字符串数据。 类型不匹配导致您的应用程序崩溃。 在使用函数之前,您必须将字符串转换为日期, 转换方法如下。

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];

注意:yyyy/MM/dd HH:mm:ss Z 是我用来显示示例的日期格式,您已根据字符串更改此格式。

//日期是你需要传递的,

你必须为你的约会都这样做,

试试这个:

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);