从类方法访问属性?
为了使我的代码可测试,我创建了一个惰性初始化程序;这样,在我的单元测试中,我可以在调用 getter 之前模拟任何我想要的对象。
但是,当涉及类方法时,我的类方法无法访问我定义的属性。
- 有什么方法可以让我的类方法访问这些属性吗?
- 如果没有,是否有任何方法可以创建也可以在此类外部访问的静态变量,即可以由我的单元测试类访问?
@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.
- Is there any way to make the properties accessible by my class method?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据定义,类方法不能有状态,这意味着它不能访问应该是“实例”一部分的变量。在实例方法(以“-”开头的方法)中,self 指针指的是调用该方法的实例,但是,在类方法(以“+”开头的方法)中,“self”指的是类本身,而不是特定实例。这意味着无法直接访问属性。
但是,执行此操作的一种方法是在实现文件中创建类的静态实例:
然后您可以使用类似“sharedInstance”方法来访问它,这样您就可以确保分配变量:
另一种选择是在实现文件中定义静态变量,然后创建类方法来从其他文件/类中设置和获取它们。
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:
then you would use something like a "sharedInstance" method to get access to it so you can make sure to allocate the variable:
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.
看起来你需要一个单身人士。
<...>
更新:
如果这种方式不可接受,以下是您问题的直接答案:
不,您必须以某种方式创建一个实例。
是的。您可以创建将保留实例的静态或全局变量。它们可以从类外部访问。静态变量可以在定义它的源文件中访问;全局变量可以从任何地方访问。
如果你想处理全局变量,
您在 *.m 文件中定义它
,并在 *.h 文件中进行声明:
Looks like you need a singleton.
<...>
Upd:
If this way isn't acceptable, here are direct answers to your questions:
No. You have to create an instance in some way.
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
and you make declaration in *.h file:
或者您可以在
.m
上声明一个静态变量,例如并声明一个方法,例如
从外部获取内部名称。这样你就不必处理外部全局变量......
or you can declare a static variable on your
.m
, likeand declare a method like
to get the internal name from outside. This way you don't have to deal with external global variables...