RQKeyedSubscript.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // RQKeyedSubscript.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/16.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. // 参数
  8. #import "RQKeyedSubscript.h"
  9. @interface RQKeyedSubscript ()
  10. /// 字典
  11. @property (nonatomic, readwrite, strong) NSMutableDictionary *kvs;
  12. @end
  13. @implementation RQKeyedSubscript
  14. + (instancetype) subscript {
  15. return [[self alloc] init];
  16. }
  17. + (instancetype) subscriptWithDictionary:(NSDictionary *) dict {
  18. return [[self alloc] initWithDictionary:dict];
  19. }
  20. - (instancetype)init {
  21. self = [super init];
  22. if (self) {
  23. self.kvs = [NSMutableDictionary dictionary];
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  28. self = [super init];
  29. if (self) {
  30. self.kvs = [NSMutableDictionary dictionary];
  31. if ([dict count]) {
  32. [self.kvs addEntriesFromDictionary:dict];
  33. }
  34. }
  35. return self;
  36. }
  37. - (id)objectForKeyedSubscript:(id)key {
  38. return key ? [self.kvs objectForKey:key] : nil;
  39. }
  40. - (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key {
  41. if (key) {
  42. if (obj) {
  43. [self.kvs setObject:obj forKey:key];
  44. } else {
  45. [self.kvs removeObjectForKey:key];
  46. }
  47. }
  48. }
  49. - (NSDictionary *)dictionary {
  50. return self.kvs.copy;
  51. }
  52. @end