objective c - Best 'init' method to set variable value for iOS 7? -
i'm still new objective c please bear me:
my app has delegate, navigation controller, , view. use singleton "global" variables.
i know can seems ungainly:
#import "globaldata.h" @synthesize ... nsinteger junk; nsinteger morejunk; -(void)mymethod{ globaldata *globdat=[globaldata getsingleton]; junk=globdat.somevalue; } -(void)myothermethod{ globaldata *globdat=[globaldata getsingleton]; morejunk=globdat.someothervalue; }
i'd can't:
#import "globaldata.h" @synthesize ... nsinteger junk; nsinteger morejunk; globaldata *globdat=[globaldata getsingleton]; //compiler won't allow line -(void)mymethod{ junk=globdat.somevalue; } -(void)myothermethod{ morejunk=globdat.someothervalue; }
however can this:
#import "globaldata.h" @synthesize ... nsinteger junk; nsinteger morejunk; globaldata *globdat; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil{ self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization globdat=[globaldata getsingleton]; } return self; } -(void)mymethod{ junk=globdat.somevalue; } -(void)myothermethod{ morejunk=globdat.someothervalue; }
so, there standard/common/proper "init" method use classes?
should in every class?
-(id)init{ if(self=[super init]){ globdat=[globaldata getsingleton]; } return self; }
i know can seems ungainly...
you seem asking how away part retrieve value want singleton. best way eliminate singleton in first place.
you have app delegate, navigation controller, , view. have view controller. if main objects in application, might consider storing data in view controller. or, convert singleton legitimate data model, , let view controller keep reference in property. can like:
-(void)mymethod{ junk = self.model.somevalue; }
which pretty close seem asking for.
Comments
Post a Comment