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: Will Mason <email@hidden>
- Date: Wed, 6 Jul 2005 12:06:12 -0700 (PDT)
> 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
A class method does not have an instance of the class on hand to which
it can refer. Therefore, there is no such thing as a myTextView in the
scope of the class. You need to have an instance of the class in order
to refer to an *instance* variable. If you pass an instance of the
class as a parameter, then you can refer to its myTextView.
Also, you're double releasing myData, as it has already been added to
the autorelease pool when you [myData release].
Cheers,
Will
_______________________________________________
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