Skip to content

Instantly share code, notes, and snippets.

@mihyaeru21
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihyaeru21/cf3d928b15812e23a9c1 to your computer and use it in GitHub Desktop.
Save mihyaeru21/cf3d928b15812e23a9c1 to your computer and use it in GitHub Desktop.
キーを変換しつつ新しい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