objective-c 如何从另一个类访问变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/658697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access variables from another class?
提问by Adam Rosenfield
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 class1I create a variable that I need to use in another class class2as well. Is there a simple way to import this variable in class2?
我的 Cocoa 文件中有多个类。在其中一个类中,class1我创建了一个变量,我也需要在另一个类中使用该变量class2。有没有一种简单的方法可以导入这个变量class2?
回答by Adam Rosenfield
You can either make the variable public, or make it into a property. For example, to make it 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];
回答by Rob
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 placeto start for object oriented programming with Objective-C.
这是使用 Objective-C 进行面向对象编程的起点。
回答by Popeye
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"];
// .....
回答by Darius Miliauskas
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! :-)
谢谢!:-)

