iphone - Objective-C for iPhone - 使用简单的日期命令时应用程序崩溃

标签 iphone objective-c cocoa-touch

我仍在学习 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/

相关文章:

iphone - "Changing the delegate of a tab bar"异常

iphone - iPhone 5 上的图像遍布整个屏幕

html - 为什么这个小的 HTML 示例在我的手机上呈现得这么小?

objective-c - 从地址簿检索数据时出现异常

objective-c - 我可以覆盖 @synthesize 在 Objective C 中的工作方式吗?

objective-c - 自动释放方法泄漏

iphone - 这个方法有什么问题吗?

iphone - self 没有被 block 捕获

ios - iPad 和 iPhone 中不同的文本标签大小

iphone - 使用具有 "opaque"属性的图像性能更好?为什么?