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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:29:20+00:00 2026-06-15T10:29:20+00:00

if you don’t take #define into account, there are two notorious ways for declaring

  • 0

if you don’t take #define into account, there are two notorious ways for declaring constants in objective-c project:

// Util.h
extern NSString * const MyConstant;

// Util.m
NSString * const MyConstant = @"value";

or the other directly in header file

// Util.h
static NSString *const MyConstant = @"value";

Now, there comes two questions:

1)
Both works, the second method is quite convenient, as I have only one place for editing values.
However as I saw from Apple .h files, the first method is always preferred, and I wonder if there are any downside, with the static method.

2) Looking at Apple docs, we often encounter very long constant name like: NSTextInputContextKeyboardSelectionDidChangeNotification. In the case you used a long constant name like that, what convention would you generally use to assign a value. If I want to use something descriptive I could use @”nsTextInputContextKeyboardSelectionDidChangeNotification”, but sounds little odd.

  • 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-06-15T10:29:21+00:00Added an answer on June 15, 2026 at 10:29 am

    I won’t explain you all the specifics of the two types of constant declaration – in short:

    The first one separates constant declaration from constant definition. Only declaration is included by header files. It also conveniently hides the actual value of the constant.

    The second one is more problematic – the header file contains both declaration and definition of the constant. That means that whenewer you include the header, the constant is created again. I don’t think this will work correctly when included from multiple files.

    The second question – no problem with long constant names. Your example is a bit extreme but there is nothing wrong with it.

    EDIT: Adding more information about the static NSString* const in header.
    Let’s have a header A.h:

    //A.h
    static NSString *const MyConstant = @"value";

    and file including it A.m

    //A.m
    #import "A.h"
    
    printA() {
       NSLog(@"A.h Constant: %@", MyConstant);
    }

    First note that headers are removed by the preprocessor before compilation. That means that before compilation there won’t be any A.h file and A.m will look like this:

    //A.m
    static NSString *const MyConstant = @"value";
    
    printA() {
       NSLog(@"A.h Constant: %@", MyConstant);
    }

    Let’s create another constant header B.h and implementation file B.m with exactly the same contents:

    //B.h
    static NSString *const MyConstant = @"value2";
    //B.m
    #import "B.h"
    
    printB() {
       NSLog(@"B.h Constant: %@", MyConstant);
    }

    Note that the constant is declared twice, with the same name and different values. This is possible because static makes the constant private for the file that includes it. If you remove the static, you’ll get a compilation error because the compiler will find two public global constants with the same name.

    In theory it is possible to use static NSString* const in header files and everything will work correctly but as you can see, it doesn’t do exactly what you want and can be a source of difficult-to-find bugs. That’s why you should use static only from implementation files.

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

Sidebar

Related Questions

Don't know how to google for such, but is there a way to query
Don't these two mean the same thing, first get the value and then increment?
Don't ask why, but is there any way to suppress a failed linking error?
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know if it's I'm doing it wrong or if there's a bug (I
Don't think there's much to say, here's my code for (int i = 0;
Don't ask how I got there, but I was playing around with some masking,
Don't know if there is a better way to do this, so that is
don't know if the title describes anything about what I'm trying to say but

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.