MKPolyline
実装したいサブブラスがありNSCoding
ます。
@interface RSRoutePolyline : MKPolyline <NSCoding>
c-arrayをエンコードする最良の方法について質問したところ、優れた回答が得られました。ただし、に定義されたinitメソッドはありませんMKPolyline
。つまり、クラスメソッド以外にデータを与える方法はありませんpolylineWithPoints:points
。
このコードは私のコメントで大丈夫ですか?
- (void)encodeWithCoder:(NSCoder *)aCoder
{
MKMapPoint *points = self.points;
NSUInteger pointCount = self.pointCount;
NSData *pointData = [NSData dataWithBytes:points length:pointCount * sizeof(MKMapPoint)];
[aCoder encodeObject:pointData forKey:@"points"];
[aCoder encodeInteger:pointCount forKey:@"pointCount"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
NSData* pointData = [aDecoder decodeObjectForKey:@"points"];
NSUInteger pointCount = [aDecoder decodeIntegerForKey:@"pointCount"];
// Edit here from @ughoavgfhw's comment
MKMapPoint* points = (MKMapPoint*)[pointData bytes];
// Is this line ok?
self = (RSRoutePolyline*)[MKPolyline polylineWithPoints:points count:pointCount];
return self;
}