In my .m file I’ve declared this variable
static NSString* MyGlobalPassword;
and in the .m file I have a class method I’m calling in another class to get an NSString
+ (NSString *) updateMessageString: (NSString *) msgString
{
MyGlobalPassword = msgString;
return MyGlobalPassword;
}
I want to access MyGlobalPassword in an instance method of the same class
- (void) Access
{
NSLog(@"I've retrieved it %@", MyGlobalPassword);
}
Objective-C throws an unrecognized selector error when I try to do this. It doesn’t want to let me access that variable in an instance method. How can I force it too?
I suspect you get the error because your static variable isn’t initialized. It’s generally not a good idea to declare variables in your .h files as they can’t be included in many places that way (without additional work and headache). You probably want to extern the declaration in your header like this:
And declare it in your .m like this:
Additionally, you usually will not want to make globals like this. Either you should have a singleton class that gets allocated once and used in many places or you should have this be a class member. You can also cheat and do the above, but make it static in the .m file and add class accessors to get at it everywhere.