从具有类级别访问权限的方法设置字符串
我想做以下事情
+(void)DoThis
{
[textView setString:@"qwertyasdfzxcvb."];
[textView setNeedsDisplay:YES];
}
但是,它拒绝设置字符串。 因为这是可行的,所以
-(void)DoThis
{
[textView setString:@"qwertyasdfzxcvb."];
[textView setNeedsDisplay:YES];
}
我得出的结论是,该字符串拒绝在具有类级别访问权限的方法中设置。那么如何允许从 +(void) 设置字符串呢?
I want to do the following
+(void)DoThis
{
[textView setString:@"qwertyasdfzxcvb."];
[textView setNeedsDisplay:YES];
}
However, It refuses to set the string.
Because this works,
-(void)DoThis
{
[textView setString:@"qwertyasdfzxcvb."];
[textView setNeedsDisplay:YES];
}
I come to the conclusion that the string refuses to be set in a method with class level access. So how do I allow a string to be set from a +(void)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设textView是一个实例变量,则无法访问它。最好的情况下,你可以
Assuming textView is an instance variable, there is no way to access it. At best, you can
您最有可能尝试做的是从类方法设置实例变量。这不起作用,因为类对象不是类的实例,因此无法访问实例变量。所以你的方法
+ (void)执行此操作
无法访问 textView 实例 变量,因为类对象不是该类的实例。
What you are most likely trying to do is set an instance variable from a class method. This does not work because class objects are not instances of the class and therefore cannot access instance variables. So your method
+ (void)DoThis
does not have access to the textView instance variable because the class object is not an instance of the class.
您不能从类方法设置实例变量。如果你想声明一个“类变量”,你必须创建一个静态全局变量 位于类的 .m 文件内,因为 Objective-C 中没有类变量。这是一个 C 静态变量,不是 Java 或 C# 所认为的意义上的变量。这里,“static”确保变量不能在该文件之外使用。
You can not set an instance variable from a class method. If you want to declare a "class variable" you have to make a static global variable inside the .m file of the class because there are no class variables in Objective-C. This is a C static variable, not in the sense Java or C# consider it. Here, "static" ensures that the variable can not be used outside of that file.
我认为您可能正在寻找的是单例/共享模式,它允许任何代码访问类的特定实例,而无需传递该实例。
这个模型在整个 Cocoa 中都使用。例如,
NSFileManager
有一个类方法defaultManager
,它返回该类的共享实例。您可以通过首先调用defaultManager
来调用此共享实例上的实例方法,因此,例如,要调用您编写的实例方法isDeletableFileAtPath
:在您的情况下,您的
DoThis 变为:
并且您需要向您的类添加一个
sharedInstance
方法。有多种方法可以做到这一点,具体取决于您希望如何使用您的类 - 它是单例吗?您只需要一个共享实例和其他非共享实例吗?您只想共享 NIB 中创建的实例吗?正如您提到的IBOutlet
这里是执行后者的实现:I think what you may be looking for is the singleton/shared pattern, which allows any code to access a particular instance of a class without being passed that instance.
This model is used throughout Cocoa. For example,
NSFileManager
has a class methoddefaultManager
which returns the shared instance of the class. You call an instance method on this shared instance by first invokingdefaultManager
, so for example to call the instance methodisDeletableFileAtPath
you write:In your case your
DoThis
becomes:And you need to add a
sharedInstance
method to your class. There a number of ways to do this depending on how you wish to use your class - Is it a singleton? Do you just need one shared instance and other non-shared ones? Do you just want to share the instance created in the NIB? As you mentionIBOutlet
here is an implementation which does the latter: