Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
キーを変換しつつ新しいNSDictionaryを返すやつ
#import <Foundation/Foundation.h>
@interface NSDictionary (MBUtil)
- (instancetype)initWithDictionary:(NSDictionary *)dictionary withKeyDictionary:(NSDictionary *)keyDictionary;
+ (instancetype)dictionaryWithDictionary:(NSDictionary *)dictionary withKeyDictionary:(NSDictionary *)keyDictionary;
@end
#import "NSDictionary+MBUtil.h"
@implementation NSDictionary (MBUtil)
- (instancetype)initWithDictionary:(NSDictionary *)dictionary withKeyDictionary:(NSDictionary *)keyDictionary
{
__block NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:dictionary.count];
[keyDictionary enumerateKeysAndObjectsUsingBlock:^(id oldKey, id newKey, BOOL *stop) {
mutableDictionary[newKey] = dictionary[oldKey];
}];
return [self initWithDictionary:mutableDictionary];
}
+ (instancetype)dictionaryWithDictionary:(NSDictionary *)dictionary withKeyDictionary:(NSDictionary *)keyDictionary
{
return [[self alloc] initWithDictionary:dictionary withKeyDictionary:keyDictionary];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.