如何从其他类访问变量?

时间:2021-11-09 15:57:19

There is probably a very simple solution for this but I can't get it working.

可能有一个非常简单的解决方案,但我不能让它工作。

I have got multiple classes in my Cocoa file. In one of the classes class1 I create a variable that I need to use in another class class2 as well. Is there a simple way to import this variable in class2?

我的Cocoa文件中有多个类。在其中一个类class1中,我创建了一个我需要在另一个类class2中使用的变量。有没有一种简单的方法在class2中导入这个变量?

4 个解决方案

#1


13  

You can either make the variable public, or make it into a property. For example, to make it public:

您可以将变量设为public,也可以将其变为属性。例如,要公开它:

@interface Class1
{
@public
    int var;
}
// methods...
@end

// Inside a Class2 method:
Class1 *obj = ...;
obj->var = 3;

To make it a property:

使它成为一个属性:

@interface Class1
{
    int var;  // @protected by default
}
@property (readwrite, nonatomic) int var;
// methods...
@end

@implementation Class1
@synthesize var;
...
@end

// Inside a Class2 method:
Class1 *obj = ...;
obj.var = 3;  // implicitly calls [obj setVar:3]
int x = obj.var;  // implicitly calls x = [obj var];

#2


6  

You could expose the variable in class2 as a property. If class1 has a reference to class2, class1 can then see the variable. Honestly, though, it sounds like you're a beginner to both Objective-C and object oriented programming. I recommend you read up more on both.

您可以将class2中的变量公开为属性。如果class1具有对class2的引用,则class1可以看到该变量。但老实说,这听起来像是Objective-C和面向对象编程的初学者。我建议你多读一下。

Here is a place to start for object oriented programming with Objective-C.

这是一个使用Objective-C开始面向对象编程的地方。

#3


3  

try making a file that holds your variables that need to be accessed throughout the app.

尝试制作一个文件,其中包含您需要在整个应用中访问的变量。

extern NSString *stringVariable;

@interface GlobalVariables

@property (retain, nonatomic) NSString *stringVariable;    

@end

and in the GlobalVariables.m file add

并在GlobalVariables.m文件中添加

#import "GlobalVariables.h"

@implements GlobalVariables

@synthesize stringVariable;

NSString *stringVariable;

@end

And then as long as you import GlobalVariables.h into which ever .m files you need to access that variable in you can assign and access anywhere throughout your program.

然后,只要您将GlobalVariables.h导入到您需要访问该变量的任何.m文件中,您就可以在整个程序中的任何位置分配和访问。

EDIT

My answer that I have given above is differently not the way I would go about doing this now. It would be more like

我上面给出的答案与我现在做的方式不同。它会更像

@interface MyClass

@property (nonatomic, strong) NSString *myVariable;

@end

then in the .m file

然后在.m文件中

@implementation MyClass

@sythesize = myVariable = _myVariable; // Not that we need to do this anymore

@end

Then in another class in some method I would have

然后在另一个类中我会有一些方法

// .....
MyClass *myClass = [[MyClass alloc] init];
[myClass setMyVariable:@"My String to go in my variable"];
// .....

#4


1  

In "XCode" you need to make import, create object by declaring it as the property, and then use "object.variable" syntax. The file "Class2.m" would look in the following way:

在“XCode”中,您需要进行导入,通过将其声明为属性来创建对象,然后使用“object.variable”语法。文件“Class2.m”将以下列方式查找:

#import Class2.h
#import Class1.h;

@interface Class2 ()
...
@property (nonatomic, strong) Class1 *class1;
...
@end

@implementation Class2

//accessing the variable from balloon.h
...class1.variableFromClass1...;

...
@end

Thanks! :-)

#1


13  

You can either make the variable public, or make it into a property. For example, to make it public:

您可以将变量设为public,也可以将其变为属性。例如,要公开它:

@interface Class1
{
@public
    int var;
}
// methods...
@end

// Inside a Class2 method:
Class1 *obj = ...;
obj->var = 3;

To make it a property:

使它成为一个属性:

@interface Class1
{
    int var;  // @protected by default
}
@property (readwrite, nonatomic) int var;
// methods...
@end

@implementation Class1
@synthesize var;
...
@end

// Inside a Class2 method:
Class1 *obj = ...;
obj.var = 3;  // implicitly calls [obj setVar:3]
int x = obj.var;  // implicitly calls x = [obj var];

#2


6  

You could expose the variable in class2 as a property. If class1 has a reference to class2, class1 can then see the variable. Honestly, though, it sounds like you're a beginner to both Objective-C and object oriented programming. I recommend you read up more on both.

您可以将class2中的变量公开为属性。如果class1具有对class2的引用,则class1可以看到该变量。但老实说,这听起来像是Objective-C和面向对象编程的初学者。我建议你多读一下。

Here is a place to start for object oriented programming with Objective-C.

这是一个使用Objective-C开始面向对象编程的地方。

#3


3  

try making a file that holds your variables that need to be accessed throughout the app.

尝试制作一个文件,其中包含您需要在整个应用中访问的变量。

extern NSString *stringVariable;

@interface GlobalVariables

@property (retain, nonatomic) NSString *stringVariable;    

@end

and in the GlobalVariables.m file add

并在GlobalVariables.m文件中添加

#import "GlobalVariables.h"

@implements GlobalVariables

@synthesize stringVariable;

NSString *stringVariable;

@end

And then as long as you import GlobalVariables.h into which ever .m files you need to access that variable in you can assign and access anywhere throughout your program.

然后,只要您将GlobalVariables.h导入到您需要访问该变量的任何.m文件中,您就可以在整个程序中的任何位置分配和访问。

EDIT

My answer that I have given above is differently not the way I would go about doing this now. It would be more like

我上面给出的答案与我现在做的方式不同。它会更像

@interface MyClass

@property (nonatomic, strong) NSString *myVariable;

@end

then in the .m file

然后在.m文件中

@implementation MyClass

@sythesize = myVariable = _myVariable; // Not that we need to do this anymore

@end

Then in another class in some method I would have

然后在另一个类中我会有一些方法

// .....
MyClass *myClass = [[MyClass alloc] init];
[myClass setMyVariable:@"My String to go in my variable"];
// .....

#4


1  

In "XCode" you need to make import, create object by declaring it as the property, and then use "object.variable" syntax. The file "Class2.m" would look in the following way:

在“XCode”中,您需要进行导入,通过将其声明为属性来创建对象,然后使用“object.variable”语法。文件“Class2.m”将以下列方式查找:

#import Class2.h
#import Class1.h;

@interface Class2 ()
...
@property (nonatomic, strong) Class1 *class1;
...
@end

@implementation Class2

//accessing the variable from balloon.h
...class1.variableFromClass1...;

...
@end

Thanks! :-)