Access property from a class method?

给你一囗甜甜゛ 提交于 2019-12-22 10:55:42

问题


In order to make my code testable, I have created a lazy initializer; this way in my unit test, I can mock any object I want before the getter gets called.

When it comes to class methods, though, my class method doesn't have access to the properties I have defined.

  1. Is there any way to make the properties accessible by my class method?
  2. If not, is there any way to create static variables that are also accessible outside of this class, i.e., accessible by my unit test class?

@implementation
@synthesize webService;

+ (void)doSomething
{
   self.webService.url = @"some url";
   [self.webService start];
   // do other things
}

- (WebService*)webService
{
   if (!webService)
   {
      webService = [[WebService alloc] init];
   }
   return webService;
}

@end

回答1:


Looks like you need a singleton.

<...>

Upd: If this way isn't acceptable, here are direct answers to your questions:

Is there any way to make the properties accessible by my class method?

No. You have to create an instance in some way.

If not is there any way to create statis variables that are also accessible outside of this class? (Accessible by my unit test class

Yes. You can create static or global variables that will keep instances. They will be accesible from outside your class. Static variable is accesible within the source file where it is defined; and global variable is accessible from everywhere. If you wand to deal with global variable, you define it in your *.m file

MyClass *my_inst;

and you make declaration in *.h file:

external MyClass *my_inst;



回答2:


By definition a class method cannot have state so that means that it can't access variables that are supposed to be a part of an "instance." In an instance method (one that starts with a "-"), the self pointer refers to the instance the method is being called on, however, in a class method (one that starts with a "+") "self" refers to the class itself, not a specific instance. This means there is no way to access properties directly.

However, one way to do this would be to create a static instance of your class within the implementation file:

static WebService* webService;

then you would use something like a "sharedInstance" method to get access to it so you can make sure to allocate the variable:

+(WebService*)sharedInstance
{
    if( nil == webService )
    {
        webService = [[WebService alloc] init];
    }
    return webService;
}

Another option is to define static variables in your implementation file, and then create class methods to set and get them from other files / classes.




回答3:


or you can declare a static variable on your .m, like

static NSString *name = @"Bob";

and declare a method like

- (NSString *)getName {
  return name;
}

to get the internal name from outside. This way you don't have to deal with external global variables...



来源:https://stackoverflow.com/questions/5771709/access-property-from-a-class-method

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