0

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)?

2
  • where is textView declared? Is it a global variable? Commented May 18, 2011 at 17:14
  • @ jojaba @property(nonatomic, retain) IBOutlet NSTextView *textView; Commented May 18, 2011 at 17:49

4 Answers 4

2

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];
}
Sign up to request clarification or add additional context in comments.

9 Comments

How would I hook aTextView in IB?
What is it that you are trying to achieve exactly that requires a class method and not an instance method?
Another file will call the method.
Can't you create an instance of the class?
Without an instance of the class holding textView, how do you think 'textView' will exist? Can you explain the broader picture of what you are trying to achieve so that we can understand this better?
|
2

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.

Comments

2

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.

4 Comments

You can't make a property static.
No, you can't. As I said, if you want to declare a "class variable" you have to make a static global variable inside the .m file
You can not set an instance variable from a class method because it does not make sense. Why not use an instance method?
Because I need to call the method from a different location or another class method.
1

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.