How do I using NSCoding for a c-array of structs? (MKPolyline)

坚强是说给别人听的谎言 提交于 2019-12-03 21:10:11
rmaddy

Note: This approach only works if the data isn't going between processors with different "endian-ness". It should be safe going from iOS to iOS, certainly if only used on a given device.

You should be able to load the memory for the C-array into an NSData object then encode the NSData object.

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

Update: to get the data back:

NSData *pointData = [aCode decodeObjectForKey:@"points"];
MKMapPoint *points = malloc(pointData.length);
memcpy([pointData bytes], points);
self.points = points;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!