ios - Getters and setters for constants? -
is possible create getters , setters constants? want refer constant directly, , have instantiate if it's value nil. constant declared this:
// prefs.h extern myclass * const kthing; // prefs.m myclass * const kthing = nil; and getter/setter like:
// getter + (myclass *)kthing { _kthing = _kthing ? : [myclass new]; return _kthing; } // setter + (void)setkthing:(myclass *)myclass { _kthing = myclass } and use like:
[kthing dosomething]; is possible?
edit edited methods class methods
global variable declaration in other file dangerous in objective c. ideally use sharedinstance. try this:
in mygame.h
@interface mygame : nsobject { int mscore; } @property(nonatomic,assign) int score; +(mygame*)sharedobject; -(void)somefunction; @end in mygame.m
static mygame *ggame = nil; @implementation mygame @synthesize score = mscore; +(mygame*)sharedobject { if(!ggame) { ggame = [[mygame alloc] init]; } return ggame; } -(void)somefunction { } @end to access anywhere in project:
#import "mygame.h" [mygame sharedobject].score; [mygame sharedobject] somefunction];
Comments
Post a Comment