在Swift中,您可以使用CGPoint
数组来存储CGPoint
对象。以下是一个示例:
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
存储在数组中。以下是一个示例:
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
对象。
领取专属 10元无门槛券
手把手带您无忧上云