Encoding c-struct with Mantle (NSCoding)

I want to use Mantle framework (https://github.com/github/Mantle) to support NSCoding for my class with struct property:

typedef struct {
    int x;
    int y;
} MPoint;

typedef struct {
    MPoint min;
    MPoint max;
} MRect;

@interface MObject : MTLModel

@property (assign, nonatomic) MRect rect;


@implementation MObject

But when I tried to [NSKeyedArchiver archiveRootObject:obj toFile:@"file"]; its crashed in MTLModel+NSCoding.m, in - (void)encodeWithCoder:(NSCoder *)coder on line

case MTLModelEncodingBehaviorUnconditional:
    [coder encodeObject:value forKey:key];

Does Mantle supports c-struct encoding (and also decoding) or I've need to custom implementing NSCoding protocol for such classes?


My original data structure is an XML (yeah, I know):


I used MTLXMLAdapter based on KissXML, but you can see how it's applicable to any other serializer.

+ (NSValueTransformer *)coordinateXMLTransformer {
    return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSArray *nodes) {
        CLLocationCoordinate2D coordinate;
        for (DDXMLNode *node in nodes) {
            if ([[node name] isEqualToString:@"Lat"]) {
                coordinate.latitude = [[node stringValue] doubleValue];
            } else if ([[node name] isEqualToString:@"Lot"]) {
                coordinate.longitude = [[node stringValue] doubleValue];

        return [NSValue value:&coordinate

You can add a reverseBlock if needed.

It was easier than I thought:

  1. Exclude property in +encodingBehaviorsByPropertyKey
  2. Manual encode/encode excluded property


#pragma mark - MTLModel + NSCoding

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.rect = [[self class] mRectFromData:[coder decodeObjectForKey:@"rectData"]];
    return self;

- (void)encodeWithCoder:(NSCoder *)coder {
    [super encodeWithCoder:coder];

    [coder encodeObject:[[self class] dataFromMRect:self.rect] forKey:@"rectData"];

+ (NSDictionary *)encodingBehaviorsByPropertyKey {
    NSDictionary *excludeProperties = @{
                                        NSStringFromSelector(@selector(rect)): @(MTLModelEncodingBehaviorExcluded)
    NSDictionary *encodingBehaviors = [[super encodingBehaviorsByPropertyKey] mtl_dictionaryByAddingEntriesFromDictionary:excludeProperties];
    return encodingBehaviors;

#pragma mark - MRect transformations

+ (MRect)mRectFromData:(NSData *)rectData {
    MRect rect;
    [rectData getBytes:&rect length:sizeof(rect)];
    return rect;

+ (NSData *)dataFromMRect:(MRect)rect {
    return [NSData dataWithBytes:&rect length:sizeof(rect)];

Need Your Help

Post to Facebook user wall using Facebook.dll in WP7

c# facebook post windows-phone-7

How to post to user wall in facebook using Facebook.dll in WP7.

Convert Method. "The specified method on the type cannot be translated into a LINQ to Entities store expression"

c# linq entity-framework postgresql

I'd migrated my entity 6.0 project from SQL Server to PostGreSQL. With SQL Server, these kind of convertion on my queries used to work properlly