Andrew
Andrew

Reputation: 3989

How to use NSNumbers to set an NSDate in a switch statement

What I want to do is map a UITableViews section index to a respective NSDate, and I originally wanted to do it like so:

-(BOOL)whatSectionsAreVisible {
    NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows];
    for (NSIndexPath *index in visibleRowIndexes) {
        NSNumber *daySection = @(index.section);

        // Here is where I will map every index.section to an NSDate
        static NSDateFormatter *dateFormatter = nil;
        if(!dateFormatter){
            dateFormatter = [NSDateFormatter new];
            dateFormatter.dateFormat = @"yyyy-MM-dd"; // Read the documentation for dateFormat
        }



        if (daySection == 0){
            NSDate *date = [dateFormatter dateFromString:@"2015-06-01"];
        }
        else if (daySection == 1){
            NSDate *date = [dateFormatter dateFromString:@"2015-06-02"];
        }

        //... and so on

}

However doing this for 30 days using if statements would get very lengthy, and I assume that using a switch statement would make more sense for this case. I'm having trouble figuring out how to setup the syntax for the switch statement, I tried doing it like this:

switch (daySection) {
            case 0:
                NSDate *date = [dateFormatter dateFromString:@"2015-06-01"];
                break;

            case 1:
                NSDate *date = [dateFormatter dateFromString:@"2015-06-02"];
                break;

            default:
                break;
        }

But the first line is giving me the error Statement requires expression of integer type ('NSNumber *__strong' invalid). How do I set this statement up correctly?

Side note: The line else if (daySection == 1) gives me a warning that I'm comparing a pointer and an integer (NSNumber and int). How would I make that comparison properly?

Upvotes: 1

Views: 333

Answers (4)

vj9
vj9

Reputation: 1554

In switch-case :

  1. Use index.section directly
  2. Use [daySection integerValue]

In if-else, NSNumber returns an object, so comparing that with 1(an integer) returns a warning.

  1. convert it to integer using [daySection integerValue]
  2. Compare the NSNumber with an object @1 using [daySection isEqualToNumber:@1] or [daySection isEqual:@1]

Upvotes: 0

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

Oh, I didn't see that at first, you cannot switch on an object. It must be an integer type, a character type, or an enumeration.

In addition, you need to change the variable declarations. Bare initializers cannot be used within a switch. Wrap the cases in curly braces { } to locally scope the variable declarations.

switch ([daySection integerValue]) {
    case 0: {
        NSDate *date = [dateFormatter dateFromString:@"2015-06-01"];
        break;
    }
    case 1: {
        NSDate *date = [dateFormatter dateFromString:@"2015-06-02"];
        break;
    }
    default:
        break;
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726929

Rather than using dateFromString initializer, build your date directly from components, and avoid switch altogether:

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:daySection.intValue]; // <<== Extract int from daySection
[comps setMonth:6];
[comps setYear:2015];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];

Upvotes: 2

Aderstedt
Aderstedt

Reputation: 6518

Don't bother converting index.section to an NSNumber; just use that as the switch argument directly, like so:

switch (index.section) {

Upvotes: 0

Related Questions