NSDictionary(Extensions) Category Reference

Declared in NSDictionary+Extensions.h

Overview

NSDictionary extensions.

+ dictionaryWithDictionary:withMapping:

Creates a new dictionary by transferring values from the given dictionary using the given { source key: destination key } mapping.

+ (nonnull instancetype)dictionaryWithDictionary:(nonnull NSDictionary *)dictionary withMapping:(nonnull NSDictionary *)mapping

Parameters

dictionary

The source dictionary to read from.

mapping

The { source key: destination key } mapping.

Return Value

The new dictionary.

Declared In

NSDictionary+Extensions.h

– dictionaryByTransformingValuesWithBlock:

Creates a new dictionary by using the given transformation block on each value.

- (nonnull NSDictionary *)dictionaryByTransformingValuesWithBlock:(nonnull id __nullable ( ^ ) ( NSString *__nonnull key , id __nonnull value ))transformBlock

Parameters

transformBlock

The block to execute on each element of the dictionary. Returning nil from the block results in the key-value being removed.

Return Value

The new dictionary with transformed elements.

Declared In

NSDictionary+Extensions.h

– randomKey

Gets a random key from the NSDictionary.

- (nullable NSString *)randomKey

Return Value

The randomly chosen key.

Declared In

NSDictionary+Extensions.h

– randomValue

Gets a random value from the NSDictionary.

- (nullable id)randomValue

Return Value

The randomly chosen value.

Declared In

NSDictionary+Extensions.h

– stringWithKey:withDefault:

Gets an NSString value with the given key, or the given default value if the value could not be retrieved.

- (nullable NSString *)stringWithKey:(nonnull NSString *)key withDefault:(nullable NSString *)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– stringWithKey:

Gets an NSString value with the given key, or nil if the value could not be retrieved.

- (nullable NSString *)stringWithKey:(nonnull NSString *)key

Parameters

key

The key to lookup the value with.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– localizedStringWithKey:

Gets a localized NSString value with the given key, or nil if the value could not be retrieved.

- (nullable NSString *)localizedStringWithKey:(nonnull NSString *)key

Parameters

key

The key to lookup the value with.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– numberWithKey:withDefault:

Gets an NSNumber value with the given key, or the given default value if the value could not be retrieved.

- (nullable NSNumber *)numberWithKey:(nonnull NSString *)key withDefault:(nullable NSNumber *)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– numberWithKey:

Gets an NSNumber value with the given key, or nil if the value could not be retrieved.

- (nullable NSNumber *)numberWithKey:(nonnull NSString *)key

Parameters

key

The key to lookup the value with.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– charWithKey:withDefault:

Gets an char value with the given key, or the given default value if the value could not be retrieved.

- (char)charWithKey:(nonnull NSString *)key withDefault:(char)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– unsignedCharWithKey:withDefault:

Gets an unsigned char value with the given key, or the given default value if the value could not be retrieved.

- (unsigned char)unsignedCharWithKey:(nonnull NSString *)key withDefault:(unsigned char)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– shortWithKey:withDefault:

Gets an short value with the given key, or the given default value if the value could not be retrieved.

- (short)shortWithKey:(nonnull NSString *)key withDefault:(short)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– unsignedShortWithKey:withDefault:

Gets an unsigned short value with the given key, or the given default value if the value could not be retrieved.

- (unsigned short)unsignedShortWithKey:(nonnull NSString *)key withDefault:(unsigned short)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– intWithKey:withDefault:

Gets an int value with the given key, or the given default value if the value could not be retrieved.

- (int)intWithKey:(nonnull NSString *)key withDefault:(int)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– unsignedIntWithKey:withDefault:

Gets an unsigned int value with the given key, or the given default value if the value could not be retrieved.

- (unsigned int)unsignedIntWithKey:(nonnull NSString *)key withDefault:(unsigned int)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– longWithKey:withDefault:

Gets an long value with the given key, or the given default value if the value could not be retrieved.

- (long)longWithKey:(nonnull NSString *)key withDefault:(long)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– unsignedLongWithKey:withDefault:

Gets an unsigned long value with the given key, or the given default value if the value could not be retrieved.

- (unsigned long)unsignedLongWithKey:(nonnull NSString *)key withDefault:(unsigned long)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– longLongWithKey:withDefault:

Gets an long long value with the given key, or the given default value if the value could not be retrieved.

- (long long)longLongWithKey:(nonnull NSString *)key withDefault:(long long)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– unsignedLongLongWithKey:withDefault:

Gets an unsigned long long value with the given key, or the given default value if the value could not be retrieved.

- (unsigned long long)unsignedLongLongWithKey:(nonnull NSString *)key withDefault:(unsigned long long)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– floatWithKey:withDefault:

Gets an float value with the given key, or the given default value if the value could not be retrieved.

- (float)floatWithKey:(nonnull NSString *)key withDefault:(float)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– doubleWithKey:withDefault:

Gets an double value with the given key, or the given default value if the value could not be retrieved.

- (double)doubleWithKey:(nonnull NSString *)key withDefault:(double)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– boolWithKey:withDefault:

Gets an BOOL value with the given key, or the given default value if the value could not be retrieved.

- (BOOL)boolWithKey:(nonnull NSString *)key withDefault:(BOOL)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– integerWithKey:withDefault:

Gets an NSInteger value with the given key, or the given default value if the value could not be retrieved.

- (NSInteger)integerWithKey:(nonnull NSString *)key withDefault:(NSInteger)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– unsignedIntegerWithKey:withDefault:

Gets an NSUInteger value with the given key, or the given default value if the value could not be retrieved.

- (NSUInteger)unsignedIntegerWithKey:(nonnull NSString *)key withDefault:(NSUInteger)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– sizeWithKey:withDefault:

Gets an CGSize value with the given key, or the given default value if the value could not be retrieved.

- (CGSize)sizeWithKey:(nonnull NSString *)key withDefault:(CGSize)def

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– dateWithKey:withDefault:interpretAsUniversalTime:

Gets an NSDate value with the given key, or the given default value if the value could not be retrieved.

- (nullable NSDate *)dateWithKey:(nonnull NSString *)key withDefault:(nullable NSDate *)def interpretAsUniversalTime:(BOOL)interpretAsUniversalTime

Parameters

key

The key to lookup the value with.

def

The default value to return in the event of a retrieval failure.

interpretAsUniversalTime

Whether the date being read is in the UTC timezone.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– enumNumberWithKey:enumType:withDefault:

Gets an enumeration value for the given enumeration type with the given key, or the given default value if the value could not be retrieved.

- (NSInteger)enumNumberWithKey:(nonnull NSString *)key enumType:(nonnull NSString *)enumType withDefault:(NSInteger)def

Parameters

key

The key to lookup the value with.

enumType

The name of the enumeration type to convert.

def

The default value to return in the event of a retrieval failure.

Return Value

The retrieved value.

Declared In

NSDictionary+Extensions.h

– mergedDictionaryWithPriorityPostfix:removeOverridesAfterApply:

Creates a new dictionary by copying the elements from this dictionary, overriding all elements with the key postfix given.

- (nonnull instancetype)mergedDictionaryWithPriorityPostfix:(nonnull NSString *)priorityPostfix removeOverridesAfterApply:(BOOL)removeOverridesAfterApply

Parameters

priorityPostfix

The key postfix to denote an override.

removeOverridesAfterApply

Removes the override elements (those with the postfix) after merge.

Return Value

The merged dictionary.

Discussion

For example, if the postfix is “iPad”, { “name”: “iPhone”, “name_iPad”: “iPad” } will be merged to { “name”: “iPad”, “name_iPad”: “iPad” }, and if removeOverridesAfterApply then { “name”: “iPad” }

Declared In

NSDictionary+Extensions.h

+ dictionaryByMergingDictionariesRespectively:

Creates a new NSDictionary object by copying respectively the contents of each dictionary in the array dictionaries.

+ (nonnull NSDictionary *)dictionaryByMergingDictionariesRespectively:(nonnull NSArray *)dictionaries

Parameters

dictionaries

An NSArray of NSDictionary objects.

Return Value

The merged NSDictionary.

Declared In

NSDictionary+Extensions.h

+ dictionaryByMergingDictionary:withDictionary:

Creates a new NSDictionary object by copying respectively the contents of dictionaryA and then dictionaryB.

+ (nullable NSDictionary *)dictionaryByMergingDictionary:(nullable NSDictionary *)dictionaryA withDictionary:(nullable NSDictionary *)dictionaryB

Parameters

dictionaryA

The first NSDictionary to copy.

dictionaryB

The second NSDictionary to copy.

Return Value

The merged NSDictionary.

Declared In

NSDictionary+Extensions.h

– mergedDictionaryWithDictionary:

Creates a new NSDictionary object by copying respectively the contents of this NSDictionary and then the given NSDictionary.

- (nonnull NSDictionary *)mergedDictionaryWithDictionary:(nonnull NSDictionary *)dictionary

Parameters

dictionary

The NSDictionary to copy.

Return Value

The merged NSDictionary.

Declared In

NSDictionary+Extensions.h

– dictionaryWithNullsRemovedRecursive:

Creates a new NSDictionary object removing all NSNull values.

- (nonnull NSDictionary *)dictionaryWithNullsRemovedRecursive:(BOOL)recursive

Parameters

recursive

Whether to recursively do the same for all child NSDictionary and NSArray values.

Return Value

The new NSDictionary with no NSNull values inside.

Declared In

NSDictionary+Extensions.h

– prunedDictionaryWithOnlyLeafKeys:

Creates a new NSDictionary object by removing all keys that aren’t in the given array of keys. This is done recursively to all child NSDictionary objects too.

- (nonnull NSDictionary *)prunedDictionaryWithOnlyLeafKeys:(nonnull NSArray<NSString*> *)onlyLeafKeys

Parameters

onlyLeafKeys

The keys to keep.

Return Value

The new NSDictionary.

Declared In

NSDictionary+Extensions.h