我似乎无法弄清楚这一点,我有一个返回 NSArray 的 Objective-C 函数,我确信 NSArray 内的数据包含 CGPoint 对象我到底如何将其转换为数组
这是函数
+(NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
{
NSMutableArray *translatedPoints = [NSMutableArray new];
// The points are provided in a dictionary with keys X and Y
for (NSDictionary *point in points) {
// Let's turn them into CGPoints
CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
// Now translate from one view to the other
CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
// Box them up and add to the array
[translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
}
return [translatedPoints copy];
}
最佳答案
您的 translatesPoints
方法返回一个 NSArray
,其中包含包装 CGPoint
的 NSValue
。让我们创建一个这样的数组:
let arr:NSArray = [NSValue(CGPoint: CGPointMake(1,2)), NSValue(CGPoint: CGPointMake(3,4))]
您可以从此数组中获取值并对它们调用CGPointValue()
:
for val in arr as [NSValue] {
let point = val.CGPointValue()
println("CGPoint = (\(point.x), \(point.y))")
}
如果需要,您可以将整个 NSArray
转换为 CGPoint
的 Swift 数组,如下所示:
let points = (arr as [NSValue]).map({$0.CGPointValue()})
现在 points
的类型为 [CGPoint]
。
关于objective-c - 任何对象到 CGPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933893/