我有一个MKPolyline
我想实现的 subblas NSCoding
,即
@interface RSRoutePolyline : MKPolyline <NSCoding>
我问了一个关于对 c 数组进行编码的最佳方法的问题,并得到了很好的答案。但是,在 上没有定义 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;
}