ios - 如何对 c 结构体数组使用 NSCoding? (MK 折线)

标签 ios objective-c cocoa-touch nscoding

我想向 c 结构数组添加 NSCoding 支持。具体来说,这是 MKPolyline 的子类。 ,即这就是我必须处理的内容:

@property (nonatomic, readonly) MKMapPoint *points;
@property (nonatomic, readonly) NSUInteger pointCount;

+ (MKPolyline *)polylineWithPoints:(MKMapPoint *)points count:(NSUInteger)count;

I found a good answer on how to encode a individual struct 。例如

NSValue* point = [NSValue value:&aPoint withObjCType:@encode(MKMapPoint)];
[aCoder encodeObject:point forKey:@"point"];

.... 

NSValue* point = [aDecoder decodeObjectForKey:@"point"];
[endCoordinateValue getValue:&aPoint];

是否有一个很好的方法将其应用于 c 数组 - 或者我只需迭代 c 数组?

最佳答案

注意:只有当数据不在具有不同“字节顺序”的处理器之间传输时,此方法才有效。从 iOS 到 iOS 应该是安全的,当然,如果仅在给定设备上使用的话。

您应该能够将 C 数组的内存加载到 NSData 对象中,然后对 NSData 对象进行编码。

MKMapPoint *points = self.points;
NSData *pointData = [NSData dataWithBytes:points length:self.pointCount * sizeof(MKMapPoint)];
[aCoder encodeObject:pointData forKey:@"points"];

更新:获取数据:

NSData *pointData = [aCode decodeObjectForKey:@"points"];
MKMapPoint *points = malloc(pointData.length);
memcpy([pointData bytes], points);
self.points = points;

关于ios - 如何对 c 结构体数组使用 NSCoding? (MK 折线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14913744/

相关文章:

ios - 获取谷歌地图的经纬度中心

ios - 获取用户好友的用户 ID 以获取应用排行榜分数

ios - UIPopoverPresentationController : multiple permittedArrowDirections order priority

ios - 我是否将相同的对象与 prepareForSegue 传回?

iOS 7 表内绝对位置响应不起作用

ios - 如何在 Swift 中切换可见性 GONE 和 VISIBLE

ios - 当数据为 NULL 时,访问链表的数据元素时应用程序崩溃

iphone - iOS 设置 UINavigationBar 标题中按钮的大小

iphone - NSMutableArray 存在内存泄漏

iphone - 如何以编程方式关闭键盘?