so i've been reading on coding best practices, , importance of using setters/getters access , change instance variables of custom classes, rather making variables public , accessing them directly.
but how translate accessing values inside mutable dictionary instance variable?
specifically, have custom class. class has number of instance variables, 1 of nsmutabledictionary. "non array" instance variables (bools, etc), understand how "properly" access them: use dot notation , setter/getter methods.
but nsmutabledictionary variable, setter/getter accesses entire dictionary, right? so, controller, how set/get values inside class dictionary?
actually, question applies code of class implementation:
take convenience init method, example:
- (id) initwithid:(int)id { if (self = [super init]) { [self initprocedure]; [mydata setobject:[nsnumber numberwithint:id] forkey:@"id"]; } return self; }
i'm accessing instance variable "mydata" directly, rather using setter/getter methods. know "allowed", understand it's not best way. how use setter/getters values within dictionary?
and then, going started, how do outside class implementation, controller, example?
for simple class instance variables, it's myclassinstance.aclassvariable = somevalue
but if want modify inside "mydata"?
do [myclassinstance.mydata setobject:... forkey:...]
(i know that's not correct, i'm trying make question/confusion clear.
i suppose can create custom class methods call within controller instead, [myclassinstance editmydatawithobject:... forkey:...]
but seems bulky, , still brings around question of implementation within class itself, , how use setter/getter technique access dictionary's member variables instead of directly modifying dictionary itself.
sorry length, hope question clear. thanks!
you can define dynamic properties access dictionary based variables:
@interface myclass : nsobject { nsmutabledictionary *mydata; } @property (nonatomic, retain) nsnumber myproperty; @end @implementation myclass @dynamic myproperty; - (nsnumber) myproperty { return [mydata objectforkey: @"myproperty"]: } - (void) setmyproperty(nsnumber *value) { [mydata setobject: value forkey: @"myproperty"]: } @end
for each variable being held in dictionary can expose content dynamic property not exposing dictionary itself. outside if feel class had dot-syntax capable properties.
Comments
Post a Comment