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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:33:54+00:00 2026-05-22T15:33:54+00:00

Possible Duplicate: Global int variable objective c I would like to create a global

  • 0

Possible Duplicate:
Global int variable objective c

I would like to create a global variable.
I want to access to this variable anywhere.

The Java equivalent:

static var score:int = 0;

For example if I define a global variables into the Game class.
How to access to this global variable?

Game.score ?
  • 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-22T15:33:55+00:00Added an answer on May 22, 2026 at 3:33 pm

    If you are having multiple views in your application, and in that case you want to have a variable accessible to every view, you should always create a Model/Data class and define the variable in it. Something like this :

    Objective-C :

    //DataClass.h      
    @interface DataClass : NSObject {    
    
        NSString *str;
    }
    
    @property(nonatomic,retain)NSString *str;    
    +(DataClass*)getInstance;    
    @end  
    
    
    //DataClass.m    
    @implementation DataClass    
    @synthesize str;
    
    static DataClass *instance = nil;
    
    +(DataClass *)getInstance
    {    
        @synchronized(self)    
        {    
            if(instance==nil)    
            {    
                instance= [DataClass new];    
            }    
        }    
        return instance;    
    }    
    

    Now in your view controller you need to call this method as :

    DataClass *obj=[DataClass getInstance];  
    obj.str= @"I am Global variable";  
    

    This variable will be accessible to every view controller. You just have to create an instance of Data class.

    Swift :

    class DataClass {
    
        private var str: String!
    
        class var sharedManager: DataClass {
            struct Static {
                static let instance = DataClass()
            }
            return Static.instance
        }
    }
    

    Usage : DataClass.sharedManager.str

    Using dispatch_once

    class DataClass {
    
        private var str: String!
    
        class var sharedInstance: DataClass {
            struct Static {
                static var onceToken: dispatch_once_t = 0
                static var instance: DataClass? = nil
            }
            dispatch_once(&Static.onceToken) {
                Static.instance = DataClass()
            }
            return Static.instance!
        }
    }  
    

    Usage : DataClass.sharedManager.str

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

Sidebar

Related Questions

Possible Duplicate: How to programmatically set a global (module) variable? I have a class
Possible Duplicate: Why can't I create an array with size determined by a global
Possible Duplicate: [PHP] global in functions Is it bad programming (PHP) practice to access
Possible Duplicate: Event listener in Java without app having focus? (Global keypress detection) I
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: Why global and static variables are initialized to their default values? What
Possible Duplicate: C programming, why does this large array declaration produce a segmentation fault?
Possible Duplicate: Javascipt exception handling is it possible to add some global exception listener

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.