0

I entered the following things, but there are some errors shown, which is expected expression in the line after case 0, and use of undeclared identifiers "bundle". Could anyone tell me what the problem is and how to solve it?

Thank you very much.

-(IBAction) segmentedControlIndexChanged{
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0:
            NSBundle *bundle = [[NSBundle alloc ]init];
            NSString *path = [bundle pathForResource:@"HK" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            break;            
        case 1:
            NSLog(@"case 1");
            break;
        case 2:
            NSLog(@"case 2");
            break;
        case 3:
            NSLog(@"case 3");
            break;
        case 4:
            NSLog(@"case 4");
            break;
        default:
            break;
    }
}

3 Answers3

4

You need some brackets so that the compiler knows what scope to apply:

case 0:
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"HK" ofType:@"plist"];
    placeArray = [[NSArray alloc] initWithContentsOfFile:path];
    break; 
}

Also, don't use alloc init on NSBundle. You either want to get the mainBundle (as above) or specifically get some other bundle in your app.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

You have a variable declaration on the first line of the case block. Wrap the block in braces:

case 0:
{
    NSBundle *bundle = [[NSBundle alloc ]init];
    NSString *path = [bundle pathForResource:@"HK" ofType:@"plist"];
    placeArray = [[NSArray alloc] initWithContentsOfFile:path];
    break;
}

See this SO question.

Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
0
NSBundle *bundle = [NSBundle mainBundle];

is the correct way.

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41