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?

勿绮语
  • 9,170
  • 1
  • 29
  • 37
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65

4 Answers4

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

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
22

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

勿绮语
  • 9,170
  • 1
  • 29
  • 37
  • 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? – Ahoura Ghotbi Nov 04 '11 at 23:36
  • 5
    Create an instance of the class and call the instance method on that instance… – Arkku Nov 04 '11 at 23:37
  • 6
    You seem not to have grasped the core concepts of OOP. Have you assimilated docs like this one? http://www.otierney.net/objective-c.html – Cyrille Nov 04 '11 at 23:42
  • @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 – Ahoura Ghotbi Nov 09 '11 at 01:16
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.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • ok so how can I create a class method to access it from other classes while I have access to the instance variables? – Ahoura Ghotbi Nov 04 '11 at 23:37
  • 3
    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`. – Cyrille Nov 04 '11 at 23:39
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;
}
SmileBot
  • 19,393
  • 7
  • 65
  • 62