Proper way to modify an NSMutableDictionary instance variable?
-
21-09-2019 - |
Question
I have an Objective-C NSMutableDictionary declared inside a class's @interface section, with getter/setter methods, like so:
@interface myClass : NSObject
{
NSMutableDictionary *dict;
}
- (void) setDict: (NSMutableDictionary *) newDict;
- (NSMutableDictionary *) dict;
Inside some of my @implementation methods I want to modify dict. I'd like to stick to standard Obj-C idiom and modify it "properly". Is it OK to modify it like this, using the getter but not the setter:
[[self dict] removeObjectForKey:@"myKey"];
...or is there a better way to do this?
Solution
That'll work, but why not do:
[dict removeObjectForKey:@"myKey"];
Inside a class's implementation, you have direct access to the instance variables, and using them directly is idiomatic Objective-C.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow