[OC Foundation框架 - 1] 常用结构体

时间:2021-01-08 07:50:56

底层封装是使用了typedef定义的结构体

typedef struct _NSString{
xxx
xxx
} NSString;
 
1. NSRange 结构体
#注意结构体不是对象
3种定义方式
         typedef struct _NSRange{
int location;
int length;
} NSRange;
(1) 先声明后赋值
(2)同时声明、按照顺序赋值
(3)使用c函数,同时声明、赋值,并指定赋值的成员变量名
     NSRange range = {, };
NSRange range2 = {.location = , .length = };
NSRange range3 = NSMakeRange(, );
 
返回对象字符串形式:
NSStringFromRange(NSRange)
转换并返回一个字符串
 
2. NSPoint / CGPoint 结构体
 void point()
{
NSPoint p;
p.x = ;
p.y = ; p = NSMakePoint(, ); p = CGPointMake(, ); NSString *str = NSStringFromPoint(p);
NSLog(@"The point is %@", str);
}
 
3. NSSize/ CGSize 结构体
 void size()
{
NSSize size;
size.width = ;
size.height = ; size = NSMakeSize(, ); size = CGSizeMake(, ); NSString *str = NSStringFromSize(size);
NSLog(@"The size is %@", str);
}
 
4. NSRect / CGRect
 void rect()
{
NSRect r;
r.origin.x = ;
r.origin.y = ;
r.size.height = ;
r.size.width = ; r = NSMakeRect(13.11, 42.11, 42.55, 11.44); r = CGRectMake(13.11, 42.11, 42.55, 11.44); }
 
origin 是一个 NSPoint