views:

94

answers:

2

Hi,

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.

+2  A: 

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.

willcodejavaforfood
+1  A: 

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