Objective-C for iPhone - application crashes when using simple date commands

I am still participating in Objective-C, so forgive me if this is a simple amateur error, but I think we all need to learn something.

Basically, I have an application with a simple bit of text in the screen title, which was IBOutletted and called 'headerText'. I want this to read February Summary, replacing February with any month — so the month should appear dynamically.

   - (void)setHeaderText {
     NSString *headerTextTitle;
     NSString *monthString;
     NSDate *month;
     NSDateFormatter *dateFormat;

     month = [[NSDate alloc] init]; // Automatically fills in today 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];
    }

I obviously can change the text and stuff, but found that the application will work when I call this method in viewDidLoad. Can someone tell me what happened? I THINK these errors on this line:

[dateFormat setDateFormat:@"MMMM"];

Because using breakpoints is a little ridiculous here. What am I doing wrong? I'm pretty confused.

!

EDIT: :

month = [[NSDate alloc] init]; // Automatically fills in today date
    dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMMM"];
    monthString = [dateFormat stringFromDate:month];

?

+3
5

undefined .

, -

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
+3

/ NSDateFormatter, ...

+1

monthString, .

. this

№1 - alloc copy, .

№2 - , .

+1

, - :

- (void) setHeaderText
{
    NSDateFormatter* formatter = [NSDateFormatter defaultFormatterBehavior];
    [formatter setDateFormat: @"MMMM"];
    headerText.text = [NSString stringWithFormat:
        @"Summary for (%@)", [dateFormat stringFromDate: [NSDate date]]];
}
+1

:

NSString *monthString = [[NSString alloc] init];

. :) !

0

Source: https://habr.com/ru/post/1731721/


All Articles