ios – 使用Mantle编码c-struct(NSCoding)

ios – 使用Mantle编码c-struct(NSCoding),第1张

概述我想使用Mantle框架( https://github.com/github/Mantle)来支持带有struct属性的NSCoding: typedef struct { int x; int y;} MPoint;typedef struct { MPoint min; MPoint max;} MRect;@interface MObject 我想使用Mantle框架( https://github.com/github/Mantle)来支持带有struct属性的NSCoding:

typedef struct {    int x;    int y;} MPoint;typedef struct {    MPoint min;    MPoint max;} MRect;@interface MObject : MTLModel@property (assign,nonatomic) MRect rect;@end@implementation MObject@end

但是当我尝试[NSKeyedArchiver archiveRootObject:obj tofile:@“file”]时;它崩溃在MTLModel NSCoding.m中,– (voID)encodeWithCoder:(NSCoder *)编码器在线

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

Mantle是否支持c-struct编码(以及解码),或者我需要为这些类自定义实现NSCoding协议?

解决方法 我的原始数据结构是XML(是的,我知道):

...  <Lat>32.062883</Lat>  <Lot>34.782904</Lot>  ...

我使用了基于KissXML的MTLXMLAdapter,但你可以看到它如何适用于任何其他序列化器.

+ (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                 withObjCType:@encode(CLLocationCoordinate2D)];    }];}

如果需要,您可以添加reverseBlock.

总结

以上是内存溢出为你收集整理的ios – 使用Mantle编码c-struct(NSCoding)全部内容,希望文章能够帮你解决ios – 使用Mantle编码c-struct(NSCoding)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存