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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:15:44+00:00 2026-06-16T05:15:44+00:00

Using objective c , I have 2 classes that are using hardware, and are

  • 0

Using objective c , I have 2 classes that are using hardware, and are written in c +objC .
The other classes in the project are objective c, and they create instance of those classes.

My question .

Lets say I have classA.m and classB.m . they both have an integer const that needs to be the same say : const int numOfSamples=7;

I am seeking for the best solution to create some configuration file, that will holds all this const variables, that both A and B can see them .
I know some ways,but I wonder whats the RIGHT thing to do .

  1. I wonder if I can just create a : configuration.m and write them into it .
  2. to use a singleton file that holds all globals .

Number 1 seems to me the best , but how exactly should I do it ?

Thanks.

  • 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-16T05:15:45+00:00Added an answer on June 16, 2026 at 5:15 am

    For approach 1 to work, you need to define two files:

    1. a header file where you declare all of your constants;

    2. a .m file where your constants are defined and initialized.

    In your example:

    /* .h file */
    extern const int numOfSamples;
    
    /* .m or .c file */
    const int numOfSamples = 7;
    

    Then, you include the .h header in every other file where you need those constants. Notice the extern keyword, this will declare the variable without also defining; in this way, you can include the .h file multiple times without having a duplicate symbol error.

    EDIT:

    The approach I suggest is the correct way to handle global variables in a C program.

    Now, if global variables are a good thing or not, well, that is a longer story.

    Generally speaking, global variables are tricky and go against a 40 years long effort towards better encapsulation (aka, information hiding) of data and behavior in a program (see “On the Criteria to Be Used in Decomposing Systems Into Modules”, David Parnas, 1972).

    To further explain this, one aspect of the problem is exactly what you mention in your comment: the possibility of one module changing the value of a global variable and thus affecting the whole behavior of the program. This is recognizedly bad and leads to uncontrollable side effects (in any non trivially sized program).

    In your case, I think things are a bit different, in that you are talking about “configuration” and “const” values. This is an altogether different case than the general one and I think you could safely use a header file of consts to that aim.

    That said, you understand that the whole theme of configuration is a huge one, in general. E.g., you could need mechanisms to change your program configuration on the fly; in this case the constant variable header approach would be not correct. Or, your program configuration could depend on the state of some remote system (imagine: you are logged in vs. you are not logged in).

    I can’t guarantee that using a header file is the best approach for your case, but I hope that the above discussion and the example I gave you can help you figure that out.

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

Sidebar

Related Questions

I am using Objective-C in my app and I have a question about multiple
I have troubles using a foreign protocol in Objective-C. Is there any other solution
I have recently started to learn Objective-C and write my tests using OCUnit that
Say i have these classes ViewA and ViewB In objective C using the delegate
I have a plugin written entirely in Python using PyObjC whose core classes I
I have two major classes in objective C using cocos2D, DebugZoneLayer and HeroClass. Using
I have been using objective-C to develop in iOS. Now we want to develop
Before I start: I'm programming for Iphone, using objective C. I have already implemented
I am new to Objective-C and iPhone I have to sort a NSDictionary using
Ok, I'm programming in objective-C and using Xcode. I have read through the documentation

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.