-1

I declare a variable and some methods in the global nsobject class like

@interface classGlobal : NSObject {
    NSString *myGuid;
}

@property(nonatomic,assign)NSString *myGuid;

and i synthesize in the .m class. but when i try to access the myGuid variable in the same class (classGlobal.m) then it shows the error "instance variable 'myGuid' accessed in class method". So please suggest how i solve this issue.

Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67

2 Answers2

3

It means that instance variables cannot be accessed from class methods. A class method is declared using a + instead of a -. If you need to use global variables I suggest you take a look at this question which answers it pretty well. And here is another one.

Community
  • 1
  • 1
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
1

The compiler complains, that you are using myGuid in a scope, where it is not accessible/defined. The declaration of myGuid in the interface part does not define a global variable, but an instance member variable. If you need a global variable (say, becaue you have to access it from a class method declared with + instead of -), declare as usual in your .m file:

MyClass.m:

    static NSString* myGuid = nil;

    + (void) someClassMethod {
        if( myGuid == nil ) ...
    }
Dirk
  • 30,623
  • 8
  • 82
  • 102