从具有类级别访问权限的方法设置字符串

发布于 2024-11-08 21:26:46 字数 361 浏览 0 评论 0原文

我想做以下事情

+(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 技术交流群。

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

发布评论

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

评论(4

回首观望 2024-11-15 21:26:46

假设textView是一个实例变量,则无法访问它。最好的情况下,你可以

+ (void)DoThis:(UITextView*)aTextView {
    [aTextView setString:@"qwertyasdfzxcvb."];
    [aTextView setNeedsDisplay:YES];
}

Assuming textView is an instance variable, there is no way to access it. At best, you can

+ (void)DoThis:(UITextView*)aTextView {
    [aTextView setString:@"qwertyasdfzxcvb."];
    [aTextView setNeedsDisplay:YES];
}
贪恋 2024-11-15 21:26:46

您最有可能尝试做的是从类方法设置实例变量。这不起作用,因为类对象不是类的实例,因此无法访问实例变量。所以你的方法
+ (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.

晨敛清荷 2024-11-15 21:26:46

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.

幸福丶如此 2024-11-15 21:26:46

我认为您可能正在寻找的是单例/共享模式,它允许任何代码访问类的特定实例,而无需传递该实例。

这个模型在整个 Cocoa 中都使用。例如,NSFileManager 有一个类方法defaultManager,它返回该类的共享实例。您可以通过首先调用 defaultManager 来调用此共享实例上的实例方法,因此,例如,要调用您编写的实例方法 isDeletableFileAtPath

[[NSFileManager defaultManager] isDeletableFileAtPath:path]

在您的情况下,您的 DoThis 变为:

+ (void) DoThis
{
    EvDudeClass *shared = [EvDudeClass sharedInstance];
    [shared.textView setString:@"qwertyasdfzxcvb."];
    [shared.textView setNeedsDisplay:YES];
}

并且您需要向您的类添加一个 sharedInstance 方法。有多种方法可以做到这一点,具体取决于您希望如何使用您的类 - 它是单例吗?您只需要一个共享实例和其他非共享实例吗?您只想共享 NIB 中创建的实例吗?正如您提到的 IBOutlet 这里是执行后者的实现:

static EvDudeClass *privateSharedInstance = nil;

- (void) awakeFromNib
{
    privateSharedInstance = self; // save this instance so it can be shared
    // other init stuff
}

+ (EvDueClass *)sharedInstance { return privateSharedInstance; }

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 method defaultManager which returns the shared instance of the class. You call an instance method on this shared instance by first invoking defaultManager, so for example to call the instance method isDeletableFileAtPath you write:

[[NSFileManager defaultManager] isDeletableFileAtPath:path]

In your case your DoThis becomes:

+ (void) DoThis
{
    EvDudeClass *shared = [EvDudeClass sharedInstance];
    [shared.textView setString:@"qwertyasdfzxcvb."];
    [shared.textView setNeedsDisplay:YES];
}

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 mention IBOutlet here is an implementation which does the latter:

static EvDudeClass *privateSharedInstance = nil;

- (void) awakeFromNib
{
    privateSharedInstance = self; // save this instance so it can be shared
    // other init stuff
}

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