// // RQKeyedSubscript.m // RQCommon // // Created by 张嵘 on 2018/11/16. // Copyright © 2018 张嵘. All rights reserved. // 参数 #import "RQKeyedSubscript.h" @interface RQKeyedSubscript () /// 字典 @property (nonatomic, readwrite, strong) NSMutableDictionary *kvs; @end @implementation RQKeyedSubscript + (instancetype) subscript { return [[self alloc] init]; } + (instancetype) subscriptWithDictionary:(NSDictionary *) dict { return [[self alloc] initWithDictionary:dict]; } - (instancetype)init { self = [super init]; if (self) { self.kvs = [NSMutableDictionary dictionary]; } return self; } - (instancetype)initWithDictionary:(NSDictionary *)dict { self = [super init]; if (self) { self.kvs = [NSMutableDictionary dictionary]; if ([dict count]) { [self.kvs addEntriesFromDictionary:dict]; } } return self; } - (id)objectForKeyedSubscript:(id)key { return key ? [self.kvs objectForKey:key] : nil; } - (void)setObject:(id)obj forKeyedSubscript:(id )key { if (key) { if (obj) { [self.kvs setObject:obj forKey:key]; } else { [self.kvs removeObjectForKey:key]; } } } - (NSDictionary *)dictionary { return self.kvs.copy; } @end