Ios 如何在NSObject类中创建私有全局变量?
我想在Ios 如何在NSObject类中创建私有全局变量?,ios,objective-c,nsobject,Ios,Objective C,Nsobject,我想在NSObject类中创建一个私有全局变量。以下是我到目前为止的情况: + (MyClass *)sharedInstance { static MyClass *sharedInstance; static dispatch_once_t once; dispatch_once(&once, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance
NSObject
类中创建一个私有全局变量。以下是我到目前为止的情况:
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
我想创建并分配一个NSDictionary
,它可以在.m
文件中访问
例如:在常规的.m UIViewController
类中,有:
@interface MyViewControllerClassName ()
// Here I would declare private global variables.
// Example:
@property (strong, nonatomic) NSArray *myArray;
@end
然后在viewdiload
中,我将为它分配一个值,并且我将能够在.m
文件中访问它
我的问题是:如何在NSObject
类中创建相同的东西(全局私有变量),而该类没有@interface
和viewDidLoad
更新
我试图在数组中添加对象,并在整个NSObject类中使用它。而不是在我使用的每个方法中创建它
例如:
NSArray *myArray = [[NSArray alloc] initWithObjects: @"One", @"Two", nil];
- (void)firstMethod {
NSLog(@"%@", [self.myArray firstObject]);
}
- (void)secondMethod {
NSLog(@"%@", [self.myArray lastObject]);
}
更新2
使用子类
NSObject
创建新文件时,.m
文件不附带@interface myObjectClass()@结束
。因此,我不知道在哪里创建我希望在整个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
@implementation MyViewControllerClassName
{
@private // << note: private is the default when declared in this scope.
NSArray * myArray;
}
@interface MyViewControllerClassName()
{
@private//我认为这是您想要用NSObject
类实现的
欢迎关注
你说:
当您使用子类NSObject
创建一个新文件时,.m
文件没有@interface myObjectClass()…@end
。因此,我不知道在哪里创建要在整个myObjectClass.m
文件中访问的变量
没错,但是没有什么可以阻止你自己添加这个(称为“私有类扩展”)。事实上,这正是你应该做的。然后,你的所有私有类属性都应该放在这个私有类扩展中
看
回到您的示例,您可能会得到一个在MyClass.h
中定义的公共接口,该接口如下所示:
@interface MyClass : NSObject
+ (MyClass *)sharedInstance;
- (void)firstMethod;
- (void)secondMethod;
@end
.m
文件可能包含定义私有属性的私有类扩展名:
@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
总之,类扩展可能不会自动为您添加,但您可以自己添加。但是如何创建NSArray
?它在.h或.m文件中吗?我可以在哪里分配数组,以便在调用其他方法之前分配它?(例如,在viewController
类中,它将位于viewDidLoad
中。但是NSObject
类没有viewDidLoad
)在NSObject
类中,viewDidLoad
的等价物是什么?我只是以ViewController
类为例。我不想从那里访问对象。我想在NSObject.m
类中声明、分配和使用数组
,因为我还有一个NSObject
类仅在类中使用访问privateObject
的方法与NSObject
类中的viewDidLoad
等效的方法是什么?init和shared之间的区别是什么…?FWIW,虽然我理解这个答案值得称赞的意图,但示例存在问题:1.使用手动定义的IVAR和@synthesis
语句不再常见。2.他正在使用ARC,因此应该使用strong
来代替retain
。3.该readonly
属性的声明缺少任何内存语义(意味着它是assign
),这不太可能是您想要的。您也不应该只包含代码示例,而应该解释每个示例的意图。最后,我建议将来使用代码块,而不是图像快照。您使用术语“私有全局变量”,而是“全局”有一个非常具体的含义,我推断你在这里并不是真正的意思。另外,你最初的例子是一个单例,但我假设你不是真的问单例,而是一般的类。对吗?@Hudson我想先学习一些基本知识,然后再学习标准问题,所有人都会很乐意提供帮助!正如我在回答中定义的那样d一切!非常感谢!!!你解释得非常好!!我只有一个问题。当我在另一个类中调用firstMethod
时,请这样做:“[[MyClass sharedInstance]firstMethod]
”或者像这样:“[[MyClass init]firstMethod]”?(注意:
sharedInstance`或init
)@Hudson您的MyClass
示例是一个单例对象(即一个对象,其中有一个且只有一个实例可以在任何地方使用),这意味着您可能想要[[MyClass sharedInstance]firstMethod]
。[MyClass init]firstMethod]
将创建一个新实例,这不太可能是您想要的。但我想退一步问一下,这个MyClass
类是什么。它到底应该是一个单例吗?单例是一个特殊情况下使用的特殊工具(有局限性和问题),所以要谨慎使用它们,但在需要它们的地方,总是使用sharedInstance
模式。如果我这样做[[MyClass sharedInstance]firstMethod]将myArray
有它的对象吗?(我的数组会有@“一”和@“二”
)当然,因为sharedInstance
最终调用了init
,它设置了myArray
。但关键是您可以从代码中的任何位置多次调用sharedInstance
,并且MyClass
将只实例化一次,因为您只处理一个共享实例(因为这是一个单身汉)。非常感谢!!你帮了我很多忙!!你很有耐心而且解释清楚
@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