iOS:日期问题

发布于 2024-11-11 14:21:49 字数 1051 浏览 4 评论 0原文

我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯 2024-11-18 14:21:49

viewDidLoad 中,您将获取一个 NSDate,您对其持有一个引用(因为您使用 init 创建了它)。第一次运行 addDay 时,您将其替换为自动释放的 NSDate,您不再为其保留引用。当您离开 addDay 时,对 dateForView 的引用将变得无效,下次您输入 addDay 并尝试增加它时,您的应用将崩溃。解决方案是:

  1. dateForView 设为具有 retain 策略的属性,
  2. viewDidLoad 中使用 self.dateForView = [NSDate date]代码>.
  3. addDay中使用self.dateForView = [self.dateForView dateByAddingTimeInterval:60*60*24*1]

另外,不要忘记在析构函数中设置 self.dateForView = nil 以避免内存泄漏。

In viewDidLoad, you are obtaining an NSDate for which you hold a reference (since you created it with init). The first time you run addDay, you replace this with an autoreleased NSDate for which you don't hold a reference any more. When you leave addDay, this reference to dateForView becomes invalid, and the next time you enter addDay and try to increment it, your app will crash. The solution is to:

  1. Make dateForView a property with retain policy,
  2. Use self.dateForView = [NSDate date] in viewDidLoad.
  3. Use self.dateForView = [self.dateForView dateByAddingTimeInterval:60*60*24*1] in addDay.

Also, don't forget to set self.dateForView = nil in your destructor to avoid leaking memory.

万水千山粽是情ミ 2024-11-18 14:21:49

可能是 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

水溶 2024-11-18 14:21:49

我已经执行了你的代码,

你必须更改这一行:-

NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];

to
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
and at last 
[formatter release];

like:-

- (IBAction) addDay{

    NSLog(@"dateforview1:%@", dateForView);
    dateForView = [dateForView dateByAddingTimeInterval:60*60*24*1];
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    [dataLabel setText:[formatter stringFromDate:dateForView]]; 
    [formatter release];

}

I have executed your code

you have to change this line:-

NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];

to
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
and at last 
[formatter release];

like:-

- (IBAction) addDay{

    NSLog(@"dateforview1:%@", dateForView);
    dateForView = [dateForView dateByAddingTimeInterval:60*60*24*1];
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    [dataLabel setText:[formatter stringFromDate:dateForView]]; 
    [formatter release];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文