如何在 NSObject class 中创建私有全局变量?
How do I create a private global variable in an NSObject class?
我想在 NSObject
class 中创建一个私有全局变量。这是我目前所拥有的:
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
我想创建并分配一个 NSDictionary
可以在整个 .m
文件中访问。
例如:在一个正则.m UIViewController
class中,有:
@interface MyViewControllerClassName ()
// Here I would declare private global variables.
// Example:
@property (strong, nonatomic) NSArray *myArray;
@end
然后在 viewDidLoad
中我会为其分配一个值,我将能够在整个 .m
文件中访问它。
我的问题是:如何在没有 @interface
和 viewDidLoad
的 NSObject
class 中创建相同的东西(全局私有变量) ]?
更新
我试图在数组中添加对象,并在我的 NSObject class 中使用它。而不是在我使用它的每个方法中创建它。
示例:
NSArray *myArray = [[NSArray alloc] initWithObjects: @"One", @"Two", nil];
- (void)firstMethod {
NSLog(@"%@", [self.myArray firstObject]);
}
- (void)secondMethod {
NSLog(@"%@", [self.myArray lastObject]);
}
更新 2
当您使用 NSObject
的子 class 创建新文件时,.m
文件不包含 @interface myObjectClass () ... @end
。因此,我不知道在哪里创建我想在整个 myObjectClass.m
文件中访问的变量。
为此,您在 sharedInstance
方法之外定义了 MyClass
对象。
static MyClass *sharedInstance = nil;
+ (MyClass *)sharedInstance
{
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
------------已编辑--------
选项 1 - MyViewControllerClassName.m
@interface MyViewControllerClassName ()
{
@private // << note: protected is the default when declared in this scope.
NSArray *myArray;
}
@end
选项 2 - MyViewControllerClassName.h
@interface MyViewControllerClassName : NSObject
{
@private // << note: protected is the default when declared in this scope.
NSArray * myArray;
}
@end
选项 3 - MyViewControllerClassName.m
@implementation MyViewControllerClassName
{
@private // << note: private is the default when declared in this scope.
NSArray * myArray;
}
@end
更多参考:Class variables explained comparing Objective-C and C++ approaches
我认为这就是您想用 NSObject
class 实现的。
热烈欢迎任何关注
你说:
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.
的确如此,但没有什么可以阻止您自己添加这个(称为 "private class extension")。事实上,这正是你应该做的。然后,您所有的私有 class 属性都应该放在这个私有 class 扩展中。
见Class Extensions Extend the Internal Implementation。
回到您的示例,您最终可能会在 MyClass.h
中定义一个 public 接口,如下所示:
@interface MyClass : NSObject
+ (MyClass *)sharedInstance;
- (void)firstMethod;
- (void)secondMethod;
@end
.m
文件可能包含定义私有属性的私有 class 扩展名:
@interface MyClass ()
@property (nonatomic, strong) NSArray *myArray;
@end
@implementation MyClass
+ (MyClass *)sharedInstance {
static MyClass *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (MyClass *)init {
self = [super init];
if (self) {
self.myArray = @[@"one", @"two"];
}
return self;
}
- (void)firstMethod {
NSLog(@"%@", [self.myArray firstObject]);
}
- (void)secondMethod {
NSLog(@"%@", [self.myArray lastObject]);
}
@end
最重要的是,class 扩展可能不会自动为您添加,但您可以自己添加。
我想在 NSObject
class 中创建一个私有全局变量。这是我目前所拥有的:
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
我想创建并分配一个 NSDictionary
可以在整个 .m
文件中访问。
例如:在一个正则.m UIViewController
class中,有:
@interface MyViewControllerClassName ()
// Here I would declare private global variables.
// Example:
@property (strong, nonatomic) NSArray *myArray;
@end
然后在 viewDidLoad
中我会为其分配一个值,我将能够在整个 .m
文件中访问它。
我的问题是:如何在没有 @interface
和 viewDidLoad
的 NSObject
class 中创建相同的东西(全局私有变量) ]?
更新
我试图在数组中添加对象,并在我的 NSObject class 中使用它。而不是在我使用它的每个方法中创建它。
示例:
NSArray *myArray = [[NSArray alloc] initWithObjects: @"One", @"Two", nil];
- (void)firstMethod {
NSLog(@"%@", [self.myArray firstObject]);
}
- (void)secondMethod {
NSLog(@"%@", [self.myArray lastObject]);
}
更新 2
当您使用 NSObject
的子 class 创建新文件时,.m
文件不包含 @interface myObjectClass () ... @end
。因此,我不知道在哪里创建我想在整个 myObjectClass.m
文件中访问的变量。
为此,您在 sharedInstance
方法之外定义了 MyClass
对象。
static MyClass *sharedInstance = nil;
+ (MyClass *)sharedInstance
{
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
------------已编辑--------
选项 1 - MyViewControllerClassName.m
@interface MyViewControllerClassName ()
{
@private // << note: protected is the default when declared in this scope.
NSArray *myArray;
}
@end
选项 2 - MyViewControllerClassName.h
@interface MyViewControllerClassName : NSObject
{
@private // << note: protected is the default when declared in this scope.
NSArray * myArray;
}
@end
选项 3 - MyViewControllerClassName.m
@implementation MyViewControllerClassName
{
@private // << note: private is the default when declared in this scope.
NSArray * myArray;
}
@end
更多参考:Class variables explained comparing Objective-C and C++ approaches
我认为这就是您想用 NSObject
class 实现的。
热烈欢迎任何关注
你说:
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 themyObjectClass.m
file.
的确如此,但没有什么可以阻止您自己添加这个(称为 "private class extension")。事实上,这正是你应该做的。然后,您所有的私有 class 属性都应该放在这个私有 class 扩展中。
见Class Extensions Extend the Internal Implementation。
回到您的示例,您最终可能会在 MyClass.h
中定义一个 public 接口,如下所示:
@interface MyClass : NSObject
+ (MyClass *)sharedInstance;
- (void)firstMethod;
- (void)secondMethod;
@end
.m
文件可能包含定义私有属性的私有 class 扩展名:
@interface MyClass ()
@property (nonatomic, strong) NSArray *myArray;
@end
@implementation MyClass
+ (MyClass *)sharedInstance {
static MyClass *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (MyClass *)init {
self = [super init];
if (self) {
self.myArray = @[@"one", @"two"];
}
return self;
}
- (void)firstMethod {
NSLog(@"%@", [self.myArray firstObject]);
}
- (void)secondMethod {
NSLog(@"%@", [self.myArray lastObject]);
}
@end
最重要的是,class 扩展可能不会自动为您添加,但您可以自己添加。