我想向 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/