[英]How to access public instance variable in Objective-C?
I am having following condition: 我有以下情况:
@interface MyClass:NSObject
@public NSString *str;
@end
@implementation
-(id)init{
}
@end
Now I want to access str variable outside MyClass in Other Class, (1) Using MyClass Object (2) without using MyClass Object, How can I achieve that? 现在,我想在其他类的MyClass外部访问str变量,(1)使用MyClass对象(2)而不使用MyClass对象,如何实现呢?
You can call using this: 您可以使用以下方式致电:
MyClass *a;
a.str;
Without the object, you cannot call an instance variable. 没有对象,就无法调用实例变量。 However, you can call static method with this declaration:
但是,您可以使用以下声明来调用静态方法:
@interface MyClass:NSObject
+ (void)doX;
@end
@implementation
+ (void)doX {
// do whatever
}
then in another class you just need to call: 然后在另一个类中,您只需要调用:
[MyClass doX];
However, let a public instance variable is not a good practice. 但是,让公共实例变量不是一个好习惯。 The reason is that it will let any class, methods change that instance variable without your control.
原因是它将允许任何类,方法在您的控制下更改该实例变量。 For example, they can set the NSString *str to nil and then nobody can call anything, or they may forget to do memory management when they call.
例如,他们可以将NSString * str设置为nil,然后没人可以调用任何东西,或者他们可能在调用时忘记进行内存管理。
A better practice for public variable is using @property
公共变量的一种更好的做法是使用
@property
For example, your string should be declared like: 例如,您的字符串应声明为:
@property (nonatomic, retain) NSString * str;
and then in the implementation: 然后在执行中:
@implementation MyClass
@synthesize str;
The good thing about property is that compiler will generate gettter and setter methods for you and those setters will handle memory correctly for you. 属性的好处是,编译器将为您生成gettter和setter方法,而这些setter将为您正确处理内存。
More about properties here 有关此处的属性的更多信息
Sigh, i realise this post is LONG dead but I believe The above answer is incorrect. igh,我知道这个帖子已经死了,但是我相信以上答案是错误的。 well the first bit.
好第一位。
Please see the link below. 请查看下面的链接。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1
for the above interface to work, you NEED to declare a property for use outside of its class Because the instance variable it is not visible outside its class. 为了使上述接口正常工作,您需要声明一个在类外部使用的属性,因为实例变量在类外部不可见。
well; 好; You don't NEED to.
您不需要。 Doing something like MyClass->str is valid.
像MyClass-> str这样的事情是有效的。
Please see this example 请看这个例子
@interface Foo : NSObject {
@public NSInteger publicMember;
@private NSInteger aproperty;
}
@property (assign) NSInteger aproperty;`
then the calling class 然后叫课
Foo *f = [Foo new];
f.aproperty = 90;
//f.publicMember = 100; property 'publicMember' not found of type Foo *
f->publicMember = 100;
But as the above post said, you should always use @properties because if var public was a string, you are not retaining the string in any way. 但是正如上面的帖子所述,您应该始终使用@properties,因为如果var public是一个字符串,则您不会以任何方式保留该字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.