NSDate ivar 成为<不是Objective-C对象>? [英] NSDate ivar become <not an Objective-C object>?
问题描述
- (id)initWithCoder:(NSCoder *)aDecoder
{
dueDate = [NSDate date];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
self.lbDueDate.text = [formatter stringFromDate:dueDate];
}
在 init 方法中,我声明了 DueDate = [NSDate date].但是当我调试时,在这一行
In init method I declared dueDate = [NSDate date]. But when I debug, at this line
self.lbDueDate.text = [formatter stringFromDate:dueDate];
并输出:(NSDate *) DueDate = 0x0c497390那么发生了什么?
And Output: (NSDate *) dueDate = 0x0c497390 So what happend ?
推荐答案
这意味着对象已经被-dealloc
ed(除非它是nil
).因此,在运行静态分析器并检查您的代码之后,与 Zombies 一起运行并更频繁地向它发送消息.
It means the object's been -dealloc
ed (unless it is nil
). So run with Zombies and message it more often -- after running the static analyzer and reviewing your code.
源码中的一个问题:dueDate = [NSDate date];
应该是dueDate = [[NSDate date] copy];
.另一个问题是,您没有在 -initWithCoder:
的实现中通过超类的指定初始值设定项进行调用.
one problem in the source: dueDate = [NSDate date];
should be dueDate = [[NSDate date] copy];
. another problem is that you don't call through the superclass' designated initializer in your implementation of -initWithCoder:
.
这篇关于NSDate ivar 成为<不是Objective-C对象>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!