如何在 Objective-C 中声明全局变量?

发布于 2024-08-21 15:07:10 字数 552 浏览 7 评论 0原文

// MyClass.h
@interface MyClass : NSObject
{
   NSDictionary *dictobj;
}
@end

//MyClass.m
@implementation MyClass

-(void)applicationDiDFinishlaunching:(UIApplication *)application
{

}
-(void)methodA
{
// Here i need to add objects into the dictionary
}

-(void)methodB
{
//here i need to retrive the key and objects of Dictionary into array
}

我的问题是,因为 methodA 和 methodB 都使用 NSDictionary 对象 [即 dictobj] 我应该在哪个方法中编写此代码:

dictobj = [[NSDictionary alloc]init];

我不能在这两种方法中执行两次,因此如何总体上执行它?

// MyClass.h
@interface MyClass : NSObject
{
   NSDictionary *dictobj;
}
@end

//MyClass.m
@implementation MyClass

-(void)applicationDiDFinishlaunching:(UIApplication *)application
{

}
-(void)methodA
{
// Here i need to add objects into the dictionary
}

-(void)methodB
{
//here i need to retrive the key and objects of Dictionary into array
}

My question is since both methodA and methodB are using the NSDictionary object [i.e dictobj] In which method should i write this code:

dictobj = [[NSDictionary alloc]init];

I can't do it twice in both methods, hence how to do it golbally?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-08-28 15:07:10

首先,如果您需要修改字典的内容,它应该是可变的:

@interface MyClass : NSObject
{
    NSMutableDictionary *dictobj;
}
@end

您通常在指定的初始化程序中创建像 dictobj 这样的实例变量,如下所示:

- (id) init
{
    [super init];
    dictobj = [[NSMutableDictionary alloc] init];
    return self;
}

并在 -dealloc 中释放内存:

- (void) dealloc
{
    [dictobj release];
    [super dealloc];
}

您可以在您的目录中的任何位置访问实例变量。实例实现(与类方法相对):

-(void) methodA
{
    // don't declare dictobj here, otherwise it will shadow your ivar
    [dictobj setObject: @"Some value" forKey: @"Some key"];
}

-(void) methodB
{
    // this will print "Some value" to the console if methodA has been performed
    NSLog(@"%@", [dictobj objectForKey: @"Some key"]);
}

First of all, if you need to modify contents of the dictionary, it should be mutable:

@interface MyClass : NSObject
{
    NSMutableDictionary *dictobj;
}
@end

You typically create instance variables like dictobj in the designated initializer like this:

- (id) init
{
    [super init];
    dictobj = [[NSMutableDictionary alloc] init];
    return self;
}

and free the memory in -dealloc:

- (void) dealloc
{
    [dictobj release];
    [super dealloc];
}

You can access your instance variables anywhere in your instance implementation (as opposed to class methods):

-(void) methodA
{
    // don't declare dictobj here, otherwise it will shadow your ivar
    [dictobj setObject: @"Some value" forKey: @"Some key"];
}

-(void) methodB
{
    // this will print "Some value" to the console if methodA has been performed
    NSLog(@"%@", [dictobj objectForKey: @"Some key"]);
}
表情可笑 2024-08-28 15:07:10
-----AClass.h-----
extern int myInt;  // Anybody who imports AClass.h can access myInt.

@interface AClass.h : SomeSuperClass
{
     // ...
}

// ...
@end
-----end AClass.h-----


-----AClass.h-----
int myInt;

@implementation AClass.h
//...
@end
-----end AClass.h-----
-----AClass.h-----
extern int myInt;  // Anybody who imports AClass.h can access myInt.

@interface AClass.h : SomeSuperClass
{
     // ...
}

// ...
@end
-----end AClass.h-----


-----AClass.h-----
int myInt;

@implementation AClass.h
//...
@end
-----end AClass.h-----
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文