从类方法访问属性?

发布于 2024-11-03 06:36:26 字数 521 浏览 0 评论 0原文

为了使我的代码可测试,我创建了一个惰性初始化程序;这样,在我的单元测试中,我可以在调用 getter 之前模拟任何我想要的对象。

但是,当涉及类方法时,我的类方法无法访问我定义的属性。

  1. 有什么方法可以让我的类方法访问这些属性吗?
  2. 如果没有,是否有任何方法可以创建也可以在此类外部访问的静态变量,即可以由我的单元测试类访问?

@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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分分钟 2024-11-10 06:36:27

根据定义,类方法不能有状态,这意味着它不能访问应该是“实例”一部分的变量。在实例方法(以“-”开头的方法)中,self 指针指的是调用该方法的实例,但是,在类方法(以“+”开头的方法)中,“self”指的是类本身,而不是特定实例。这意味着无法直接访问属性。

但是,执行此操作的一种方法是在实现文件中创建类的静态实例:

static WebService* webService;

然后您可以使用类似“sharedInstance”方法来访问它,这样您就可以确保分配变量:

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

另一种选择是在实现文件中定义静态变量,然后创建类方法来从其他文件/类中设置和获取它们。

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.

抚笙 2024-11-10 06:36:27

看起来你需要一个单身人士。

<...>

更新:
如果这种方式不可接受,以下是您问题的直接答案:

有什么办法可以让
我的班级可以访问的属性
方法?

不,您必须以某种方式创建一个实例。

如果没有的话有什么办法可以创建
统计变量也
在这个类之外可以访问吗?
(可以通过我的单元测试类访问

是的。您可以创建将保留实例的静态或全局变量。它们可以从类外部访问。静态变量可以在定义它的源文件中访问;全局变量可以从任何地方访问。
如果你想处理全局变量,
您在 *.m 文件中定义它

MyClass *my_inst;

,并在 *.h 文件中进行声明:

external MyClass *my_inst;

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;
一个人的夜不怕黑 2024-11-10 06:36:27

或者您可以在 .m 上声明一个静态变量,例如

static NSString *name = @"Bob";

并声明一个方法,例如

- (NSString *)getName {
  return name;
}

从外部获取内部名称。这样你就不必处理外部全局变量......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文