我仍在学习 Objective-C,所以如果这是一个简单的业余错误,请原谅我,但我想我们都必须以某种方式学习。
基本上我有一个应用程序,在屏幕的标题处有一些简单的文本,它已经被 IBOutletted 并称为“headerText”。我希望它显示为“二月摘要”,将二月替换为任何月份 - 因此必须动态获取月份。
- (void)setHeaderText {
NSString *headerTextTitle;
NSString *monthString;
NSDate *month;
NSDateFormatter *dateFormat;
month = [[NSDate alloc] init]; // Automatically fills in today's date
[dateFormat setDateFormat:@"MMMM"];
monthString = [dateFormat stringFromDate:month];
headerTextTitle = [[NSString alloc] initWithFormat:@"Summary for (%@)", monthString];
headerText.text = headerTextTitle;
[headerTextTitle release];
[monthString release];
[month release];
[dateFormat release];
}
我显然可以修改文本和内容,但我发现每当我在 viewDidLoad 上调用此方法时应用程序都会崩溃。谁能告诉我怎么了?我认为这里的这一行有错误:
[dateFormat setDateFormat:@"MMMM"];
因为当使用断点时,那里的东西会变得有点滑稽。我究竟做错了什么?我很困惑。
感谢您的帮助!
jack
编辑:我现在正在这样做:
month = [[NSDate alloc] init]; // Automatically fills in today's date
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM"];
monthString = [dateFormat stringFromDate:month];
但是还是失败了?
最佳答案
一开始您的 dateFormat 是未定义的。
你需要初始化它,比如
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
关于iphone - Objective-C for iPhone - 使用简单的日期命令时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2215208/