如何使用NSCoding作为结构的c数组? (MKPolyline)

我想将NSCoding支持添加到一个c数组结构中。 具体来说,这是为MKPolyline的子类,即这是我必须处理:

 @property (nonatomic, readonly) MKMapPoint *points; @property (nonatomic, readonly) NSUInteger pointCount; + (MKPolyline *)polylineWithPoints:(MKMapPoint *)points count:(NSUInteger)count; 

我发现如何编码一个单独的结构很好的答案 。 例如

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

有没有一种很好的方式将其应用于ac数组 – 或者我只需要遍历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;