objective-c - 任何对象到 CGPoint

标签 objective-c swift

我似乎无法弄清楚这一点,我有一个返回 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,其中包含包装 CGPointNSValue。让我们创建一个这样的数组:

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/

相关文章:

ios - tableView.reloadData() 添加了一个它不应该做的 UIView

ios - 如何更改二维码的颜色

swift - 用 Metal 加载纹理的首选方法

swift - 在uitableview swift中同时添加两行

iphone - 如何为地址簿中的联系人创建具有多种类型的 NSMutableDictionary?

iphone - 如何绘制矩形?

objective-c - 从数组中抓取项目时崩溃... objective-c

ios - 无法删除 uicollectionviewcell 底部的间距?

ios - 快速加载 xib 文件的正确方法

iphone - 如何在 objective-c 中每 24 小时更新一次应用程序