Latest 0.2.1
Homepage https://github.com/PlusR/ManagedMappingObject
License MIT
Platforms ios 6.0, requires ARC
Authors ,

NSManagedObject <-> NSDictionary (e.g. JSON Model class)

Converting NSManagedObject to NSDictionary (and back again).

Installation

pod 'ManagedMappingObject'

Usage

1 Setup NSManagedObject class.

You can easily generate using mogenerator.

mogenerator -m ManagedMappingObject.xcdatamodeld -O CoreDataModels 
    --base-class ManagedMappingObject 
    --template-var arc=true
  • Target NSManagedObject class must be subclass of ManagedMappingObject.
  • Target NSManagedObject class must implement <ManagedMappingProtocol> .

2 Create NSValueTransformer subclass if need betransformed value.

  • UnitTransformer in example case.
  • implement + (NSDictionary *)JSONValueTransformerNames of <ManagedMappingProtocol>.

e.g.)

+ (NSDictionary *)JSONKeyMap {
    return @{
        UnitAttributes.identifier : @"id",
        UnitAttributes.date : @"unix_time",
        UnitAttributes.centimeter : @"meter"
    };
}

+ (NSDictionary *)JSONValueTransformerNames {
    return @{
        UnitAttributes.centimeter : NSStringFromClass([MeterToCentimeterTransformer class]),
        UnitAttributes.date : NSStringFromClass([UnixTimeToNSDateTransformer class])
    };
}

+ (NSString *)entityName {
    return [super entityName];
}

3 You can use following method in NSManagedObject, after setup.

// NSDictionary -> NSManagedObject
+ (instancetype)insertNewWithDictionary:(NSDictionary *) dictionary managedObjectContext:(NSManagedObjectContext *) context;
// update NSManagedObject
- (void)updateWithDictionary:(NSDictionary *) dictionary;
// NSManagedObject -> NSDictionary
- (NSDictionary *)dictionaryRepresentation;

NOTE: These methods ignore nil.

Example

See Example and Tests for details on.

Bonus : NSDictionary Model

Raw NSDictionary is difficult to deal with,
so this example project use JSON Accelerator.

fooJSONModel class has following useful methods :

- (instancetype)initWithDictionary:(NSDictionary *)dict;
- (NSDictionary *)dictionaryRepresentation;

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

Acknowledgment

Thanks @ishkawa

Latest podspec

{
    "name": "ManagedMappingObject",
    "version": "0.2.1",
    "summary": "Converting NSManagedObject to NSDictionary (and back again).",
    "homepage": "https://github.com/PlusR/ManagedMappingObject",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "azu": "[email protected]",
        "PlusR": "[email protected]"
    },
    "platforms": {
        "ios": "6.0"
    },
    "source": {
        "git": "https://github.com/PlusR/ManagedMappingObject.git",
        "tag": "0.2.1"
    },
    "requires_arc": true,
    "subspecs": [
        {
            "name": "Core",
            "source_files": "src/MappingService.{h,m}"
        },
        {
            "name": "ManagedMappingObject",
            "source_files": "src/ManagedMappingObject.{h,m}",
            "frameworks": "CoreData",
            "dependencies": {
                "ManagedMappingObject/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This