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 Answers 3

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.

0
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.

0
0
NSBundle *bundle = [NSBundle mainBundle];

is the correct way.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.