Re: Referencing an instance variable in a class method
Re: Referencing an instance variable in a class method
- Subject: Re: Referencing an instance variable in a class method
- From: James Spencer <email@hidden>
- Date: Wed, 6 Jul 2005 14:18:33 -0500
On Jul 6, 2005, at 1:53 PM, Julio Cesar Silva dos Santos wrote:
I am trying to access an instance variable within a class method
but I receive a warning: "instance variable accessed in class
method". Is there a way to get around this warning? The code is
something like this:
@interface myClass : NSObject {
IBOutlet id myTextView;
}
+(void)myClassMethod:(NSString*)myPath;
@end
@implementation
+(void)myClassMethod:(NSString*)myPath {
NSData * myData = [NSData dataWithContentsOfFile:myPath];
//Here pops up the warning
[myTextView replaceCharactersInRange:NSMakeRange(0,[[myTextView
string] length]) withRTF:myData];
[myData release];
}
@end
Why am I doing this? Because I am writing an application that uses
AppleScript, but AS does not work fine with RTF files the way
Objective-C does. Then I use the "call method" to (obviously) call
a method inside a class but as far as I know this call only works
for class methods, hence the myClassMethod declaration.
Your question would have been better asked on the AppleScript Studio
list but to answer your questions.
No, there is no way to work around this. By definition, class
methods refer to the class generally, not any specific instance of
the class but instance variables by definition exist only for a
particular instance. The class does not have any particular instance
of myTextView. Put another way, in the generalized class method, how
can replaceCharactersInRange know where to replace the characters.
However, you are mistaken that call method only works for class
methods. Rather, there are are two versions
call method of class
call method of object
The first calls a class method, for example, NSString's
stringWithFormat: method which does not care if you have any
instances of a string already in existence but rather simply creates
a string and returns it to you.
The second calls a method for a particular object (i.e. a particular
instance of a class), for example NSString's stringByAppendingString:
method which obviously requires an already existing string (both
because this is defined as an instance method so the compiler
requires it but also on a pratical level, if you didn't already have
a string, what would you append to.
There is a two other versions of call method but both are variations
of call method of object and call instance methods, not class methods:
call method of
call method
The first is simply a simple version of call method of object
The second specifically calls a method of the application delegate
object (if the method exists there) or of the application object if
it doesn't.
See http://developer.apple.com/documentation/AppleScript/Reference/
StudioReference/sr3_app_suite/chapter_3_section_22.html
for more information.
James P. Spencer
Rochester, MN
email@hidden
"Badges?? We don't need no stinkin badges!"
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden