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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:19:54+00:00 2026-05-15T12:19:54+00:00

What is the easiest way to create a global object. I have tried declaring

  • 0

What is the easiest way to create a global object. I have tried declaring the object outside the method with no luck.

@implementation UV_TouchpadViewController;
NSMutableString *string = [NSMutableString stringWithFormat:@"text"];
  • 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-15T12:19:55+00:00Added an answer on May 15, 2026 at 12:19 pm

    Very close — you can’t initialize a non-local variable with a non-const expression, and a method call is inherently non-const, even if it looks like it should be. So basically, change it to

    NSMutableString *string;
    

    but if it’s only going to be used inside the implementation file (eg. other classes would only get at it through UV_TouchpadViewController, not get/set it directly (this is also the recommended pattern)), then qualify it as static, like so

    static NSMutableString *string;
    

    If on the other hand you do want to be able to access it directly from outside UV_TouchpadViewController, leave off the static, but add

    extern NSMutableString *string;
    

    to your header file (outside the class @interface), and whomever includes the header will be able to access it. (Note that you could instead just put NSMutableString *string; in your header file, however this is quickly becomes unclear)

    Also, if you are trying to do this for a singleton class, (I can’t think of a good reason to have a global mutable string — you know they’re not thread safe right?) I recommend reading Apple’s docs on singletons first, where they suggest you use ivars, not global variables, even for singletons. However, UV_TouchpadViewController should not even be a singleton (if it is in any way a view controller), it should just have a single instance, if that’s all you want.

    If on the other hand you just want all UV_TouchpadViewControllers to have access to this one variable, note that across almost all languages this is considered a bad design pattern (globals are bad), and that you should instead stick it in, say, your app delegate (which is guaranteed to have a single globally accessible instance), where it can be an ivar+accessors, and generally considered a setting and (with a little extra code) persisted.

    EDIT:

    If you want to have a singleton that maintains global state, which I still recommend against — you should create a class, like for instance ApplicationState, which handles all of the application’s global state as a model object in the traditional model-view-controller pattern. I wont go into detail here because that would be highly redundant of a google search.

    In your Application Delegate, somewhere, add an ivar ApplicationState *state, and a corresponding @property (and @synthesize in the implementation file).

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

Sidebar

Related Questions

I'm looking for suggestions on the easiest way to create charts and have them
What's the easiest way to create an array of structs in Cocoa?
What is the easiest way to create a Silverlight Button with custom Content which
What's the easiest way to create a simple HTTP server with Java? Are there
What's the easiest way to create drop shadows around div boxes? A print media
What would be the easiest way to create a C compiler for a custom
What's the easiest way to quickly create some simple HTML in Python? All I've
Just wondering what the easiest way to connect 2 jsp pages together, I have
What is the easiest way to create a form layout like (say) this one:
What is the easiest way to create a web service with XML response? Use

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.