5

I am developing an application in which I implement a class method.h How can I access an instance variable in a class method?

Here is my instance method and an object is created of the class 'SomeClass':

-(void)viewDidLoad
{
   SomeClass *obj=[SomeClass alloc] init];
}

Here is the class method and in this method I am trying to access instance variable 'obj', so I got an error saying "instance variable 'obj' accessing in class method".

+(void)classMethodImplement
{

}
4
  • The way you are defining it there means it is only in function scope? Commented Aug 19, 2012 at 11:23
  • is there any alternate solution please tell me sir Commented Aug 19, 2012 at 11:25
  • 1
    The basic problem is that you can have a thousand (or a million) instances of the class, so how do you decide which one to reference? Commented Aug 19, 2012 at 14:05
  • How can you say thousand? I can guarantee 100% if that class was the view controller of the main view of the app, then it will be only one instance, and it will be allocated at launch. This instance will never be dealloc'd (until the app terminates). Commented Aug 20, 2012 at 14:01

3 Answers 3

15

You can't access instance variables from class methods. As instance variables are bound to certain instances of a class, accessing them from a class method doesn't make sense, thus the language doesn't permit it.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but this is just crazy. I access instance variables using class methods all the time in my prototyping apps. I use the idea that I posted, and it works, and thus instance variables can be accessed through class methods. Even if you have many instances, it is possible to define a static array that stores all instances allocated. Is that a good idea? That's not the issue here, leave that to the asker to decide.
@Mazyod of course it's possible but you have to have very good reasons in order it not to be bad practice. In my opinion, if you have to access instance variables from class methods this way, your design is flawed (NB I didn't downvote, however).
10

There is an obvious way of doing it. You just have to use static variables.

Option 1:

Store the instance variable that you want into a static variable:

static id static_iVar = nil;
- (void)viewDidLoad {
    // blah, blah, blah
    static_iVar = _iVar;
}

Option 2:

Store self into a static variable, then access its iVars as you like:

static id static_self = nil;
- (void)viewDidLoad {
    // blah, blah, blah
    static_self = self;
}

In either option, please be extremely careful. If you allocate two instances of the same class without doing proper checking and stuff, no one knows what kind of bugs could appear.

Secondly, just use the static variables in the static method:

+(void)classMethodImplement
{
    // Use it here...
}

Comments

4

Rewrite the class method so that its caller supplies the necessary object.

+(void)classMethodImplement:(SomeClass *)obj
{
}

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.