Inherits from NSObject
Declared in EFMapper.h
EFMapper.m

Overview

EFMapper maps data such as those coming from JSON onto an instance using mappings. The mappings are also used to simplify implementing the NSCoding protocol for a class, and to create a dictionary representation of an instance.

Override these methods when implementing custom EFMapper subclasses.

Tasks

Singleton

Mappings

Initializers

Validating and applying values

NSCoding support

Dictionary representation

Other Methods

Class Methods

sharedInstance

Convenience singleton

+ (instancetype)sharedInstance

Return Value

A EFMapper instance

Discussion

Convenience singleton

You don’t have to use EFMapper as a singleton, but it can often be convenient.

Declared In

EFMapper.h

Instance Methods

decodeObject:withCoder:

Decodes an instance using mappings

- (void)decodeObject:(id)object withCoder:(NSCoder *)aDecoder

Parameters

object

The object

aDecoder

The NSCoder object from the -[initWithCoder:] method

Discussion

Decodes an instance using mappings

The coding confirms to NSSecureCoding, so you may wish to return YES from -[ requiresSecureCoding] in your subclass.

Declared In

EFMapper.h

dictionaryRepresentationOfObject:

Creates a dictionary representation

- (id)dictionaryRepresentationOfObject:(id)object

Parameters

object

The object

Return Value

Dictionary representation of the object

Discussion

Creates a dictionary representation

The dictionary is created using the external keys as registered with the mapper, or if none registered those registered for the mappings of the class. For classes without any mappings, the object is included as is. If a value is nil, or its reverse transformation fails, the key will be set to NSNull.

Declared In

EFMapper.h

dictionaryRepresentationOfObject:forKeys:

Creates a dictionary representation

- (id)dictionaryRepresentationOfObject:(id)object forKeys:(NSArray *)keys

Parameters

object

The object

keys

The keys to include in the dictionary, pass nil to include all

Return Value

Dictionary representation of the object

Discussion

Creates a dictionary representation

The dictionary is created using the passed in array of keys. For classes without any mappings, the object is included as is. If a value is nil, or its reverse transformation fails, the key will be set to NSNull.

Declared In

EFMapper.h

encodeObject:withCoder:

Encodes an instance using mappings

- (void)encodeObject:(id)object withCoder:(NSCoder *)aCoder

Parameters

object

The object

aCoder

The NSCoder object from the -[encodeWithCoder:] method

Discussion

Encodes an instance using mappings

Declared In

EFMapper.h

objectOfClass:withValues:error:

Initializes an object and applies values

- (id)objectOfClass:(Class)aClass withValues:(NSDictionary *)values error:(NSError **)error

Parameters

aClass

Class of object

values

The values to be applied

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

New object if all values are valid, nil otherwise

Discussion

Initializes an object and applies values

Declared In

EFMapper.h

registerDictionaryRepresentationKeys:forClass:

Registers the keys that should be included in a dictionary representation

- (void)registerDictionaryRepresentationKeys:(NSArray *)keys forClass:(Class)aClass

Parameters

keys

Array of NSString keys

aClass

Class of object

Discussion

Registers the keys that should be included in a dictionary representation

By default all external keys defined in the mappings for the class are included.

Declared In

EFMapper.h

registerInitializer:forClass:

Register initializer to be called when the mapper needs to instantiate a class

- (void)registerInitializer:(EFMappingInitializerBlock)initializerBlock forClass:(Class)aClass

Parameters

initializerBlock

Block returning class instance

aClass

Class for which the initializer should be used

Discussion

Register initializer to be called when the mapper needs to instantiate a class

The initializer is also used for subclasses of the class, unless a more initializer mappings for that subclass is registered. If no initializer is registed, alloc and init will be called on the class.

Declared In

EFMapper.h

registerMapper:forClass:

Mapper to be used for setting values on instances of a class

- (void)registerMapper:(EFMapper *)mapper forClass:(Class)aClass

Parameters

mapper

EFMapper instance

aClass

Class for which the mapper should be used

Discussion

Mapper to be used for setting values on instances of a class

You can register another EFMapper instance, e.g. a specialized subclass, to handle mappings of specific classes. The mapper is also used for subclasses of the class, unless more specific mapper for that subclass is registered.

Declared In

EFMapper.h

registerMappings:forClass:

Mappings to be used for setting values on instances of a class

- (void)registerMappings:(NSArray *)mappings forClass:(Class)aClass

Parameters

mappings

Array of EFMapping instances

aClass

Class for which the mappings should be used

Discussion

Mappings to be used for setting values on instances of a class

Mappings are also used for subclasses of the class, unless more specific mappings for that subclass are registered.

Declared In

EFMapper.h

setValue:onObject:isCollection:mapping:

Applies incoming value to an object

- (void)setValue:(id)value onObject:(id)object isCollection:(BOOL)isCollection mapping:(EFMapping *)mapping

Parameters

value

Incoming transformed and validated value

object

Object on which the value should be set

isCollection

Indicates wether the incoming value is a collection

mapping

EFMapping instance

Discussion

Applies incoming value to an object

Declared In

EFMapper-Subclass.h

setValues:onObject:error:

Apply values to an instance

- (BOOL)setValues:(NSDictionary *)values onObject:(id)object error:(NSError **)error

Parameters

values

The values to be applied

object

The object

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

YES if all values are valid, NO otherwise

Discussion

Apply values to an instance

First validates the values, and if all are found to be valid applies them on the instance of a class using its mappings.

Declared In

EFMapper.h

transformValue:mapping:reverse:error:

Transforms (if needed) any incoming value

- (id)transformValue:(id)value mapping:(EFMapping *)mapping reverse:(BOOL)reverse error:(NSError **)error

Parameters

value

Incoming value

mapping

EFMapping instance

reverse

Perform any transformation in reverse

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

Transformed value or nil if error occurred

Discussion

Transforms (if needed) any incoming value

Declared In

EFMapper-Subclass.h

validateValue:isCollection:mapping:error:

Validates an incoming value

- (BOOL)validateValue:(id)value isCollection:(BOOL)isCollection mapping:(EFMapping *)mapping error:(NSError **)error

Parameters

value

Incoming transformed value

isCollection

Indicates wether the incoming value is a collection

mapping

EFMapping instance

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

YES if value is valid, otherwise NO

Discussion

Validates an incoming value

Declared In

EFMapper-Subclass.h

validateValues:forClass:error:

Validate values to be applied to an instance of a class

- (BOOL)validateValues:(NSDictionary *)values forClass:(Class)aClass error:(NSError **)error

Parameters

values

The values to be validated

aClass

Class of object

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

YES if all values are valid, NO otherwise

Discussion

Validate values to be applied to an instance of a class

Verifies wether the values in a dictionary can be successfully applied on the instance of a class using its mappings. This basically means that values get checked on wether they are of the right type.

Declared In

EFMapper.h

validateValues:onObject:error:

Validate values to be applied to an instance

- (BOOL)validateValues:(NSDictionary *)values onObject:(id)object error:(NSError **)error

Parameters

values

The values to be validated

object

The object

error

On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

YES if all values are valid, NO otherwise

Discussion

Validate values to be applied to an instance

Verifies wether the values in a dictionary can be successfully applied on the instance of a class using its mappings. This basically means that values get checked on wether they are of the right type. In this method instances also get a chance to validate a value using the standard KVO validation. To use this implement -[validate<Key>:error:] method in your class.

Declared In

EFMapper.h