I want to create a private global variable in an NSObject
class. Here is what I have so far:
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
I want to create and assign an NSDictionary
that can be accessed throughout the .m
file.
For example: In a regular .m UIViewController
class, there is:
@interface MyViewControllerClassName ()
// Here I would declare private global variables.
// Example:
@property (strong, nonatomic) NSArray *myArray;
@end
Then in viewDidLoad
I would assign it a value, and I will be able to access it throughout the .m
file.
My question is: How can I create the same thing (a global private variable) in an NSObject
class, which doesn't have @interface
and viewDidLoad
?
Update
I'm trying to add objects in an array, and use it throughout my NSObject class. Instead of creating it in every method that I'm using it in.
Example:
NSArray *myArray = [[NSArray alloc] initWithObjects: @"One", @"Two", nil];
- (void)firstMethod {
NSLog(@"%@", [self.myArray firstObject]);
}
- (void)secondMethod {
NSLog(@"%@", [self.myArray lastObject]);
}
Update 2
When you create a new file with a subclass of NSObject
, the .m
file doesn't come with @interface myObjectClass () ... @end
. Therefore, I don't know where to create my variables that I want to access throughout the myObjectClass.m
file.