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

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

typedef struct _NSString{
xxx
xxx
} NSString;
 
1. NSRange 结构体
#注意结构体不是对象
3种定义方式
1         typedef struct _NSRange{
2             int location;
3             int length;
4         } NSRange;

 

(1) 先声明后赋值
(2)同时声明、按照顺序赋值
(3)使用c函数,同时声明、赋值,并指定赋值的成员变量名
1     NSRange range = {1, 2};
2     NSRange range2 = {.location = 1, .length = 2};
3     NSRange range3 = NSMakeRange(1, 2);
 
返回对象字符串形式:
NSStringFromRange(NSRange)
转换并返回一个字符串
 
2. NSPoint / CGPoint 结构体
 1 void point()
 2 {
 3     NSPoint p;
 4     p.x = 1;
 5     p.y = 2;
 6    
 7     p = NSMakePoint(4, 2);
 8    
 9     p = CGPointMake(11, 33);
10    
11     NSString *str = NSStringFromPoint(p);
12     NSLog(@"The point is %@", str);
13 }
 
3. NSSize/ CGSize 结构体
 1 void size()
 2 {
 3     NSSize size;
 4     size.width = 100;
 5     size.height  = 90;
 6    
 7     size = NSMakeSize(42, 55);
 8    
 9     size = CGSizeMake(63, 77);
10    
11     NSString *str = NSStringFromSize(size);
12     NSLog(@"The size is %@", str);
13 }
 
4. NSRect / CGRect
 1 void rect()
 2 {
 3     NSRect r;
 4     r.origin.x = 10;
 5     r.origin.y = 12;
 6     r.size.height = 44;
 7     r.size.width = 62;
 8 
 9     r = NSMakeRect(13.11, 42.11, 42.55, 11.44);
10    
11     r = CGRectMake(13.11, 42.11, 42.55, 11.44);
12    
13 }
 
origin 是一个 NSPoint
 
posted @ 2014-11-24 19:00  HelloVoidWorld  阅读(316)  评论(0编辑  收藏  举报