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"];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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
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
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
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).