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 6129097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:42:10+00:00 2026-05-23T16:42:10+00:00

So I have an issue with using a constant variable in the following switch

  • 0

So I have an issue with using a constant variable in the following switch statement in Objective-C.

I have Constants.h with the following:

// Constants.h    
extern NSInteger const TXT_NAME;

And Constants.m as:

// Constants.m
#import "Constants.h"

NSInteger const TXT_NAME        = 1;

Then in TabBasic.m I am trying to use this constant in a switch-case statement:

// TabBasic.m

#import "TabBasic.h"
#import "Constants.h"

... code ...

- (IBAction)saveValue:(id)sender {
    if ([sender isKindOfClass: [UITextField class]]) {
        UITextField *txtField = (UITextField *) sender;

        switch (txtField.tag) {
            case TXT_NAME:
                NSLog(@"Set property name to: %@", txtField.text); 
                break;
        }
    }
}

But unfortunately it is giving me the following two errors on the “case TXT_NAME:” line:

  • Expression is not an integer constant expression
  • Case label does not reduce to an integer constant

Does anyone know what I’m doing wrong? The “tag” variable of a UITextField returns an NSInteger, so I don’t see the issue…

Thanks for your help!

  • 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-23T16:42:10+00:00Added an answer on May 23, 2026 at 4:42 pm

    Quick solution, you should place NSInteger const TXT_NAME = 1; in Constants.h, and don’t need anything in Constants.m.

    Reason: If you set the value of the constant in the .m, it is not visible by other translation units that only include the .h file. The value of the constant must be known at compile time to be able to be used in a case within a switch.

    Update:

    The above works when compiling in Objective-C++. You need to have your files end in .mm instead of .m for them to be compiled in Objective-C++ instead of Objective-C.

    In order to work in Objective-C, you should define your constant either like this:

    #define TXT_NAME 1

    Or even better, like this:

    enum {TXT_NAME = 1};

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following issue using java 1.4 I try to display a very
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have an issue with using AWK to simply remove a field from a
I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but
I have an issue that is driving me a bit nuts: Using a UserProfileManager
I have this strange issue with my web app. You see, I'm using jQuery
i have issue regarding Paging using filterexpression. here's the piece of code for filtering
I have an issue regarding conversion from float to c++ string using ostringstream. Here
I'm using crystal reports 10 and have an issue with a duplex printer. Basically
In using our TeamCity Continuous Integration server we have uncovered some issues that we

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.