【发布时间】:2011-06-01 09:30:52
【问题描述】:
我有这个代码:
在viewDidLoad:
dateForView = [[NSDate alloc] init]; (dateForView is a NSDate)
还有一个 IBAction:
- (IBAction) addDay{
NSLog(@"dateforview1:%@", dateForView);
dateForView = [dateForView dateByAddingTimeInterval:60*60*24*1];
NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];
[dataLabel setText:[formatter stringFromDate:dateForView]];
}
当我按下连接到此 IBAction 的按钮时,第一次一切正常,但下一次它崩溃了。这是控制台崩溃的结果:
2011-06-01 11:29:55.238 Prenotazioni[554:707] dateforview1:(
"<UIControlTargetAction: 0x1962d0>"
)
2011-06-01 11:29:55.246 Project[554:707] -[__NSArrayI dateByAddingTimeInterval:]: unrecognized selector sent to instance 0x1ba680
2011-06-01 11:29:55.264 Project[554:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI dateByAddingTimeInterval:]: unrecognized selector sent to instance 0x1ba680'
【问题讨论】:
-
一个需要考虑的问题与您的问题无关。简单地将 86,400 秒添加到日期可能不是添加一天的正确答案,因为它不适合 DST 更改。您可能应该使用 NSDateComponents developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…
-
我知道 nsdatecomponent,但是在与 NSDate 比较之后我会遇到问题
-
您阅读我提供的链接了吗?您创建一个代表某一天的 NSDateComponent,然后使用
NSCalendar将其添加到dateForView。dateForView仍然是NSDate。
标签: iphone objective-c xcode ios nsdate