iOS:日期问题
我有这样的代码:
in 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'
I have this code:
in viewDidLoad
:
dateForView = [[NSDate alloc] init]; (dateForView is a NSDate)
and a 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]];
}
When I push a button connected to this IBAction, it's all ok the first time but it crashes the next time around. This is the result of crash in console:
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
viewDidLoad
中,您将获取一个NSDate
,您对其持有一个引用(因为您使用init
创建了它)。第一次运行addDay
时,您将其替换为自动释放的NSDate
,您不再为其保留引用。当您离开addDay
时,对dateForView
的引用将变得无效,下次您输入addDay
并尝试增加它时,您的应用将崩溃。解决方案是:dateForView
设为具有retain
策略的属性,viewDidLoad
中使用self.dateForView = [NSDate date]
代码>.addDay
中使用self.dateForView = [self.dateForView dateByAddingTimeInterval:60*60*24*1]
。另外,不要忘记在析构函数中设置 self.dateForView = nil 以避免内存泄漏。
In
viewDidLoad
, you are obtaining anNSDate
for which you hold a reference (since you created it withinit
). The first time you runaddDay
, you replace this with an autoreleasedNSDate
for which you don't hold a reference any more. When you leaveaddDay
, this reference todateForView
becomes invalid, and the next time you enteraddDay
and try to increment it, your app will crash. The solution is to:dateForView
a property withretain
policy,self.dateForView = [NSDate date]
inviewDidLoad
.self.dateForView = [self.dateForView dateByAddingTimeInterval:60*60*24*1]
inaddDay
.Also, don't forget to set
self.dateForView = nil
in your destructor to avoid leaking memory.可能是 dateForView 已发布。为了解决这个问题,请使用 [dateForView keep];在ibaction中。但是这样会增加内存
May be the dateForView is released. For solving this use the [dateForView retain]; in the ibaction.But this will increase the memory
我已经执行了你的代码,
你必须更改这一行:-
I have executed your code
you have to change this line:-