我想将MKMapPoint转换为NSValue。在目标C中,我可以通过以下声明来做到这一点:
MKMapPoint point = MKMapPointForCoordinate(location.coordinate);
NSValue *pointValue = [NSValue value:&point withObjCType:@encode(MKMapPoint)];我怎样才能在斯威夫特做到这一点?谢谢!
发布于 2015-09-14 13:18:20
可悲的是,这在Swift中目前是不可能的。
发布于 2016-04-21 05:42:02
这在Swift中是不可能的,但是您仍然可以在ObjC中创建一个类别并在Swift项目中使用它
// NSValue+MKMapPoint.h
@interface NSValue (MKMapPoint)
+ (NSValue *)valueWithMKMapPoint:(MKMapPoint)mapPoint;
- (MKMapPoint)MKMapPointValue;
@end
// NSValue+MKMapPoint.m
@implementation NSValue (MKMapPoint)
+ (NSValue *)valueWithMKMapPoint:(MKMapPoint)mapPoint {
return [NSValue value:&mapPoint withObjCType:@encode(MKMapPoint)];
}
- (MKMapPoint)MKMapPointValue {
MKMapPoint mapPoint;
[self getValue:&mapPoint];
return mapPoint;
}
@endSwift中的用法:
let mapValue = CGValue(MKMapPoint: <your map point>)
let mapPoint = mapValue.MKMapPointValue();发布于 2019-04-17 11:03:00
我认为Leo的答案已经不正确了,我设法将MKMapPoint数组转换为CGPoint数组,代码如下:
let polygonView = MKPolygonRenderer(overlay: overlay)
let polyPoints = polygonView.polygon.points() //returns [MKMapPoint]
var arrOfCGPoints : [CGPoint] = []
for i in 0..<polygonView.polygon.pointCount {
arrOfCGPoints.append(polygonView.point(for: polyPoints[i])) //converts to CGPoint
}
print(arrOfCGPoints)
//prints [(10896.74671715498, 10527.267575368285), (10830.46552553773, 10503.901612073183), (10741.784851640463, 10480.270403653383), (10653.04738676548, 10456.62348484993), (10566.442882657051, 10409.803505435586)]对于NSValue:
ler someCgPoint = arrOfCGPoints[0]
var pointObj = NSValue(CGPoint: someCgPoint)https://stackoverflow.com/questions/32454230
复制相似问题