我想初始化的NSDate與特定的日期,在我所有的代碼使用它的一個我這樣做:的NSDate爲@財產給我bad_exc_access
.H
@property (nonatomic, retain) NSDate *myDate;
.M
@synthesize myDate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
myDate = [formatter dateFromString:@"2050-01-01"];
}
return self;
}
然後在代碼中,如果我這樣讀了NSDate的,如:
NSLog(@"%@",myDate);
或者如果我使用isEqualToDate,給我一個exc_bad_access
爲什麼?
注意,在'初始化使用存取的危險.. .'方法:http://stackoverflow.com/questions/192721/why-shouldnt-i-use-objective-c-2-0-accessors-in-init-dealloc – petert