Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3360742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:01:44+00:00 2026-05-18T03:01:44+00:00

I get a compiler error when using an Objective-C object within a switch statement:

  • 0

I get a compiler error when using an Objective-C object within a switch statement:

switch (myConstant)
{
    case 0:
        UIViewController *myController = [[[UIViewController alloc] init] autorelease];
        [self.navigationController pushViewController:myViewController animated:YES];
        break;
    case 1:
        // stuff
        break;
    default:
        break;
}

The error states:

Expected expression before ‘UIViewController’
‘myViewController’ undeclared (first use in this function)

I understand that the second error is a direct result of the first error, but what I don’t understand is why I get the ‘expected expression’ error in the first place…

If I put a ; at the end of the case 0: line, then it will compile, but I shouldn’t have to do this, no?

This will also fail to compile, with the same error:

switch (0)
{
    case 0:
        int a = 0;
        break;
    default:
        break;
}

However, if I declare the int a; outside of the switch block, then a = 0; compiles fine.

I thought I understood switch statements – clearly I don’t. Could someone please explain?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:01:45+00:00Added an answer on May 18, 2026 at 3:01 am

    Just add {} within the case if you declare a new variable. The case part of a switch statement is not a correct scope to declare variables in C.

     case 0:
         {
            int a = 0;
            break;
        }
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try using a Name=id and\or x:name=id I get a compiler error: The
I get an error when I compile this code: using System; public struct Vector2
I get the following error when I try to compile an asp.net site using
I wanted to use boost::thread in my program, but get the following compiler error
Why do I get compiler errors with this Java code? 1 public List<? extends
When trying to compile my class I get an error: The constant 'NamespaceName.ClassName.CONST_NAME' cannot
When attempting to compile my C# project, I get the following error: 'C:\Documents and
With the Visual Studio 2005 C++ compiler , I get the following warning when
Ok, so I'm using Objective-C. Now, say I have: TopClass : NSObject - (int)
I want to use the NSStatusBar object from my Java application using JNI. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.