objective-c 如何从Objective-C中的另一个类访问@public实例变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2182439/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:52:13  来源:igfitidea点击:

How to access @public instance variable from another class in Objective-C?

objective-cpublic-fields

提问by Eonil

I know it's possible to define public instance variable with @public keyword. However, Objective-C syntax does not allow accessing other class' variable. What features should I expected from @public Ivar? Or how do I access other class' Ivars?

我知道可以使用 @public 关键字定义公共实例变量。但是,Objective-C 语法不允许访问其他类的变量。我应该期待@public Ivar 提供哪些功能?或者我如何访问其他班级的 Ivars?

回答by Jonathan Sterling

Objective-C, as a superset of C, definitely does allow the access of public instance variables from outside the class's implementation. Now, the reason you may have heard that it isn't allowedis that it is highly discouraged. In most cases, if you want to access an instance variable outside an implementation context, you should be using accessors and mutators (properties).

Objective-C 作为 C 的超集,确实允许从类的实现外部访问公共实例变量。现在,您可能听说不允许这样做的原因是非常不鼓励这样做。在大多数情况下,如果要访问实现上下文之外的实例变量,则应该使用访问器和修改器(属性)。

An Objective-C class really boils down to a plain-old C struct with an isafield (that's what makes it an object), where the public fields are accessible. Since when we are dealing with instances of classes, we are working a pointer to an object (special struct). Thus, we access public fields using ->.

Objective-C 类实际上归结为带有isa字段的普通 C 结构(这就是使其成为对象的原因),其中公共字段是可访问的。因为当我们处理类的实例时,我们正在处理一个指向对象(特殊结构)的指针。因此,我们使用 访问公共字段->

Here's an example:

下面是一个例子:

@interface SomebodyIsntEncapsulating : NSBadIdea {
  @public
  NSString *badIdea;
  BOOL shouldntDoIt;

  @protected
  NSString *ahThatsBetterThankGod;

  @private
  NSString *sweetThanksNowEvenMySubclassesCantTouchMe;
}

Now, in some completely different context, we could have:

现在,在一些完全不同的上下文中,我们可以:

SomebodyIsntEncapsulating *whatOh = [[SomebodyIsntEncapsulating alloc]
                                       initWithDanger:kDangerLevelEpicBraceYourself];
whatOh->badIdea = [@"I'm a public field. Make sure to retain or copy me!" copy];
NSLog(@"Please! Write some accessors and mutators!: %@", whatOh->badIdea);

I hope that helped you!

我希望对你有帮助!

回答by Vladimir

You can access it like using -> (member by pointer) operator like you normally access members of a c-structure (note that you always have a pointer to an object in obj-c):

您可以像使用 -> (member by pointer) 运算符一样访问它,就像您通常访问 c 结构的成员一样(请注意,您总是有一个指向 obj-c 中的对象的指针):

...
@public
int a;
...

myObject->a = 1;

回答by Costique

You are supposed to write accessor methods to do that. Object-oriented design implies that a class shouldn't care about internal structure of objects of another class.

您应该编写访问器方法来做到这一点。面向对象的设计意味着一个类不应该关心另一个类的对象的内部结构。

That said, idis just a pointer, so you can do obj->ivar, provided you know what you're doing and there is no way to write a proper accessor method.

也就是说,id只是一个指针,因此您可以执行 obj->ivar,前提是您知道自己在做什么并且无法编写正确的访问器方法。