20

I have a variable declared in the header file :

@interface

int _nPerfectSlides;

and

@property (nonatomic, readwrite) int _nPerfectSlides;

and I have a method that I declared in the header :

+ (void) hit;

The method has the following code in it :

+ (void) hit {
    NSLog(@"hit");
    _nPerfectSlides = 0;
    [_game showHit];
}

now for some reason I get the error "Instance variable '_nPerfectSlides' accessed in class method" error and it seems like I cant access any variables inside the method. What am I doing wrong?

4 Answers 4

25

1. For + (void)hit:Only have access to the self object.

--Step 1: Remove follwing line from header file

@property (nonatomic, readwrite) int _nPerfectSlides;

--Step 2:

  • Add int _nPerfectSlides in your class file globally..
  • That means declare before @implementation

Eg: In .m File

#import "Controller.h"
int _nPerfectSlides // Add like this before @implementation

@implementation Controller

2. For - (void)hit:Only have access to the instance methods

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

Comments

22

If you meant to make this an instance method, change that + to -.

4 Comments

Yea I tried that before posting, it fixed this issue but when I try calling the method from another class it crashes... what shall I do?
Create an instance of the class and call the instance method on that instance…
You seem not to have grasped the core concepts of OOP. Have you assimilated docs like this one? otierney.net/objective-c.html
@Cyrille you are right, I am new to OOP tbh, used to structured coding. I am reading a few docs around the net and trying to figure it out and also shape up my OOP. but thanks anyways
9

An instance variable is, as its name suggests, only accessible in the instance methods (those declared with -). Class methods (declared with +) have no access to instance variable, no more than they have access to the self object.

2 Comments

ok so how can I create a class method to access it from other classes while I have access to the instance variables?
I don't understand your comment. Either you change your + (void) hit into a - (void) hit, or you declare _nPerfectSlides on a global level, outside of your class' @interface.
4

I know this is old, but it still comes up. Try making it a static. Here's I'm altering the code a bit to make it increment.

// Hit.h

#import <Foundation/Foundation.h>
@interface Hit : NSObject
+ (void)hit;
@end

// Hit.m

#import "Hit.h"
@implementation Hit
static int val = 0;
+ (void)hit {
    val += 1;
    [self showHit];
}
+ (void)showHit {
    NSLog(@"hit value: %d", val);
}
@end

//main.m

#import <Foundation/Foundation.h>
#import "Hit.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [Hit hit];
        [Hit hit];
        [Hit hit];
    }
    return 0;
}

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.