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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:24:18+00:00 2026-05-29T16:24:18+00:00

i would like to do some order to my self about the globals. if

  • 0

i would like to do some order to my self about the globals.

  1. if i write in my .m file

    int ran //is it global? every class can see it ?
    
    @implementation
    @end
    
  2. if i am writing in my .h file this:

    int ran //is it global to any other class that include this .h ?
    @interface
    {
    }
    
  3. if i am writing in my .h file this:

    extern int ran //is it global to any other class that include this .h ?
    @interface
    {
    }
    
  4. if i am writing in my .h file this:

    @interface
    {
    int ran //can every other class that include it can see it ?
    }
    

    AND about the @interface, every thing that in the interface is belong to that class only?
    what about *NSString that in the @interface? is it belong to that class only ?
    can i use it when including this .h? or do i have to make an object of that class to see it ?

What is the best way to put a global variable of a primitive , that only my class will see it and can be use it ?

  • 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-29T16:24:19+00:00Added an answer on May 29, 2026 at 4:24 pm
    1. If you write (in any compilation unit == .m file)

      int ran;
      

      Fine, defines a global variable, initialized to zero, symbol visible to the linker.

    2. If you write in a header file

      int ran;
      

      That’s a bad idea, since every implementation file (.m) that includes the header would define it’s own instance of the global variable. Linker confusion would arise.

    3. If you write in a header file

      extern int ran;
      

      Fine, declares a global variable. Must be defined in one implementation file (.m).

    4. If you write in a header file

      @interface Foo : NSObject
      {
          int ran;
      }
      

      That’s ok, too. It declares an Objective-C class with an instance variable ran. The instance variable would be created for and belong to each instance of Foo. The instance variable is @protected (by default) and can thus be accessed directly from any deriving class. This would be bad design, though.

      Nowadays you would not declare instance variables in the @interface of a class but in the @implementation or just @synthesizethem.

    If you need a private global variable, define it in you implementation file using:

    static int ran;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get some nested params. I have an Order that has
I am using elemntFromPoint in order to return onclick some element. I would like
I would like do some thing like this: SELECT sum(quantity) FROM orders WHERE order_code
I would like some help with the following regexes. 00 should be replaced by
I would like some advice on the best approach to use in the following
I would like some of my preferences to have icons, like the Settings app.
I am developing a site and i would like some simple markup. I would
I am learning C++ exceptions and I would like some clarification of the scenario:
I am designing RESTful Api's and would like some advice on designing an API
Ok, so I would like some advice on how to set up a model.

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.