一:先看一下,在c语言中定义的结构体:
//定义了Date这种结构体类型
struct Date{
int year;
int month;
int day;
};
//定义结构体变量
struct Date d={2014,7,4};
NSLog(@"year is %i,month is %i,day is %i",d.year,d.month,d.day);
d.day=6;
NSLog(@"day is %i",d.day);
如上事例中的
struct Date{
int year;
int month;
int day;
};
定义了一个结构体类型。定义变量时:
struct Date d={2014,7,4};
使用:d.year,d.month,d.day
我们也可以使用typedef来定义一个结构体类型:如:
typedef struct {
int year;
int month;
int day;
} MyDate;
MyDate d={2014,7,4};
NSLog(@"year is %i,month is %i, day is %i",d.year,d.month,d.day);
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
NSRange range;
range.location=10;
range.length=4;
b, 第二种,应用c语言的聚合结构赋值机制
NSRangerange={8,3};
c, 第三种,应用Foundation框架提供的一个快捷函数NSMakeRange
NSRange range=NSMakeRange(12,3);
使用,可以直接通过调用变量名引用,也可以将变量转换成字符串输出,比如:
通过变量名称引用:
NSLog(@"location is %zi",range.location);//其中z代表无符号
将变量转换成字符串输出,调用了NSString中的一个函数 NSStringFromRange
NSString *str=NSStringFromRange(range);
2,NSPoint的介绍:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef structCGPoint CGPoint;
其中CGPoint和NSPoint是相等的
这个结构体代表的时平面中的一个点(x,y)
NSPoint p;
p.x=8;
p.y=10;
c,调用CGPointMake函数创建
NSString *str=NSStringFromPoint(p);
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef structCGSize CGSize;
NSSize size;
size.width=100;
size.height=80;
NSSizesize=NSMakeSize(50,60);
NSSizesize=CGSizeMake(40,50);
NSString *str=NSStringFromSize(size);
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef structCGRect CGRect;
NSRect rect;
rect.size.height=8;
rect.size.width=10;
rect.origin.x=40;
rect.origin.y=50;
NSRect rect=NSMakeRect(100,80,10,8);
NSRect rect=CGRectMake(100,80,10,8);
使用:NSString *str=NSStringFromRect(rect);