首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在阵列中存储CGPoint

在Swift中,您可以使用CGPoint数组来存储CGPoint对象。以下是一个示例:

代码语言:swift
复制
var cgPointArray: [CGPoint] = []

// 添加CGPoint对象到数组中
let point1 = CGPoint(x: 10, y: 20)
cgPointArray.append(point1)

let point2 = CGPoint(x: 30, y: 40)
cgPointArray.append(point2)

// 访问数组中的CGPoint对象
print("第一个点的坐标:\(cgPointArray[0])")
print("第二个点的坐标:\(cgPointArray[1])")

在Objective-C中,您可以使用NSValue对象将CGPoint存储在数组中。以下是一个示例:

代码语言:objective-c
复制
NSMutableArray *cgPointArray = [NSMutableArray array];

// 添加CGPoint对象到数组中
CGPoint point1 = CGPointMake(10, 20);
NSValue *point1Value = [NSValue valueWithCGPoint:point1];
[cgPointArray addObject:point1Value];

CGPoint point2 = CGPointMake(30, 40);
NSValue *point2Value = [NSValue valueWithCGPoint:point2];
[cgPointArray addObject:point2Value];

// 访问数组中的CGPoint对象
CGPoint firstPoint = [[cgPointArray objectAtIndex:0] CGPointValue];
CGPoint secondPoint = [[cgPointArray objectAtIndex:1] CGPointValue];
NSLog(@"第一个点的坐标:{%f, %f}", firstPoint.x, firstPoint.y);
NSLog(@"第二个点的坐标:{%f, %f}", secondPoint.x, secondPoint.y);

请注意,在使用NSValue时,您需要使用valueWithCGPoint方法将CGPoint对象转换为NSValue对象,并使用CGPointValue方法将NSValue对象转换回CGPoint对象。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券