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

不打扰是莪最后的温柔 提交于 2019-11-28 08:26:04

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 allowed is 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).

An Objective-C class really boils down to a plain-old C struct with an isa field (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 ->.

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!

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):

...
@public
int a;
...

myObject->a = 1;

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, id is 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!