Instance variable 'variable' accessed in class method error

后端 未结 4 1976
长发绾君心
长发绾君心 2020-12-14 09:03

I have a variable declared in the header file :

@interface

int _nPerfectSlides;

and

@property (nonatomic, readwri         


        
4条回答
  •  执念已碎
    2020-12-14 09:27

    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 
    @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 
    #import "Hit.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            [Hit hit];
            [Hit hit];
            [Hit hit];
        }
        return 0;
    }
    

提交回复
热议问题