前言
typedef CGPoint NSPoint;
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
1、NSPoint 结构体变量的创建与调用
// NSPoint 结构体变量的创建与赋值
// 先定义变量,再赋值
NSPoint point1;
point1.x = 6;
point1.y = 1;
// 定义时直接赋值
NSPoint point2 = {7, 2};
// 给指定成员赋值
NSPoint point3 = {.y = 3, .x = 8};
// 使用函数赋值
NSPoint point4 = NSMakePoint(9, 4);
// 使用等价的结构体定义,等价于 CGPoint point5 = CGPointMake(10, 5);
NSPoint point5 = CGPointMake(10, 5);
// NSPoint 结构体变量值的调用
NSLog(@"point1: %.0f, %.0f", point1.x, point1.y);
NSLog(@"point2: %.0f, %.0f", point2.x, point2.y);
NSLog(@"point3: %.0f, %.0f", point3.x, point3.y);
NSLog(@"point4: %.0f, %.0f", point4.x, point4.y);
NSLog(@"point5: %.0f, %.0f", point5.x, point5.y);
2、NSPoint 与 NSString 的相互转换
// NSPoint 转 NSString
NSString *stringFronPoint = NSStringFromPoint(point5);
// NSString 转 NSPoint
NSPoint point6 = NSPointFromString(stringFronPoint);