我想将NSCoding支持添加到结构的c数组中。具体来说,这是 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;