Trouble with NSdate
I am writing a program that has multiple views. I want to pass a date variable between views. I made a global variable using extern NSDate *chooseDate
. I declare the variable NSDate *dat开发者_如何学编程e
on one of the views. I then set date = chooseDate
. I then use the following code to add days to date;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:(int)daycount];
date = [calendar dateByAddingComponents:components toDate:date options:0];
The problem seems to be that chooseDate
is not being recognized as an NSDate
. The code crashes at date = [calendar ...]
. I am at a loss as to why this is happening. I hope I have explained the clearly. Any ideas?
Probably it will work if you change chooseDate = [datePicker date];
to chooseDate = [[datePicker date] copy];
. If it does, the rest should be pretty much self explanatory.
You're setting chooseDate
to an autoreleased
instance. Are you sure it exists when you try to use it? You may need to retain
it.
精彩评论