前言
-
结构体 这个结构体用来表示事物的坐标点和宽高度。
typedef CGRect NSRect; struct CGRect { CGPoint origin; CGSize size; }; typedef struct CGRect CGRect;
1、NSRect 结构体变量的创建与调用
// NSRect 结构体变量的创建与赋值
// 先定义变量,再赋值
NSRect rect1;
rect1.origin.x = 6;
rect1.origin.y = 1;
rect1.size.width = 1;
rect1.size.height = 6;
// 定义时直接赋值,或者 NSRect rect2 = {{7, 2}, {2, 7}};
NSRect rect2 = {7, 2, 2, 7};
// 给指定成员赋值
NSRect rect3 = {.size.height = 8, .size.width = 3, .origin.y = 3, .origin.x = 8};
// 使用函数赋值
NSRect rect4 = NSMakeRect(9, 4, 4, 9);
// 使用等价的结构体定义,等价于 CGRect rect5 = CGRectMake(10, 5, 5, 10);
NSRect rect5 = CGRectMake(10, 5, 5, 10);
// NSRect 结构体变量值的调用
NSLog(@"rect1: %.0f, %.0f, %.0f, %.0f", rect1.origin.x, rect1.origin.y, rect1.size.width, rect1.size.height);
NSLog(@"rect2: %.0f, %.0f, %.0f, %.0f", rect2.origin.x, rect2.origin.y, rect2.size.width, rect2.size.height);
NSLog(@"rect3: %.0f, %.0f, %.0f, %.0f", rect3.origin.x, rect3.origin.y, rect3.size.width, rect3.size.height);
NSLog(@"rect4: %.0f, %.0f, %.0f, %.0f", rect4.origin.x, rect4.origin.y, rect4.size.width, rect4.size.height);
NSLog(@"rect5: %.0f, %.0f, %.0f, %.0f", rect5.origin.x, rect5.origin.y, rect5.size.width, rect5.size.height);
2、NSRect 与 NSString 的相互转换
// NSRect 转 NSString
NSString *stringFronRect = NSStringFromRect(rect5);
// NSString 转 NSRect
NSRect rect6 = NSRectFromString(stringFronRect);