请用以下代码帮助我。我想做的就是看到两个日期之间的时间(4:42:55)相差不到24小时,所以我不在乎日期。

谢谢

NSLog(@"error code");
NSDate *now = [[NSDate alloc] init];
NSDate *time = [[NSDate date] dateFromString:@"6:23 PM"];
NSTimeInterval secs = [now timeIntervalSinceDate:time];
NSLog(@"end error code");

int hours = floor((int)secs /  (60 * 60) );
float minute_divisor = (int)secs % (60 * 60);
int minutes = floor(minute_divisor / 60);
float seconds_divisor = (int)secs % 60;
secs = ceil(seconds_divisor);
NSString *msg = [NSString stringWithFormat: @"%@:%@:%@", hours, minutes, (int)secs];

这里有日志
2011-08-24 22:17:34.827 App[31814:f503] error code
2011-08-24 22:17:34.827 App[31814:f503] -[__NSDate dateFromString:]: unrecognized selector sent to instance 0x6e23d30
2011-08-24 22:17:34.828 App[31814:f503] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate dateFromString:]: unrecognized selector sent to instance 0x6e23d30'
*** First throw call stack:
(0x1664082 0x17f5d0a 0x1665d1d 0x15caf70 0x15cad52 0x4998 0x392a 0xdef05 0x3f8d3 0x3fb27 0x3fc82 0xc94ec46 0x46b7a 0x23ae 0x179a0 0x18870 0x275f3 0x280a8 0x1ba29 0x130a10d 0x16381f5 0x159d0a2 0x159b98a 0x159ae34 0x159ad4b 0x18271 0x19a1b 0x1ff8 0x1f55)
terminate called throwing an exception

最佳答案

dateWithString: 是一个类方法:

[NSDate dateWithString:...]

但是,iOS上的NSDate没有此方法。您必须将NSDateFormatter与 -dateFromString: 方法一起使用。

关于ios - -[NSDate dateFromString:]:无法识别的选择器发送错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7184811/

10-14 20:45