OC开发之——Foundation中常用结构体(52)

一 概述

本文介绍Foundation框架中的结构体:

  • NSRange
  • NSPoint\CGPoint
  • NSSize\CGSize
  • NSRect\CGRect

二 NSRange

2.1 定义

1
2
3
4
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;

2.2 使用

1
2
3
4
5
6
7
8
NSRange rg1={2,4};
NSRange rg2={.location=2,.length=4};
NSRange rg3=NSMakeRange(2, 4);

NSString *str=@"i love oc";
NSRange range=[str rangeOfString:@"love"];
NSLog(@"loc=%ld,length=%ld",range.location,range.length);
NSLog(@"NSNotFound=%d",NSNotFound);

三 NSPoint\CGPoint

3.1 说明(NSPoint==CGPoint)

1
typedef CGPoint NSPoint;

3.2 使用

1
2
3
NSPoint p1=NSMakePoint(10, 10);
CGPoint p2 =CGPointMake(10, 10);
//CGPointZero==CGPointMake(0, 0);

四 NSSize\CGSize

4.1 定义

1
2
3
4
struct CGSize {
CGFloat width;
CGFloat height;
};

4.2 使用

1
2
3
//NSSize\CGSize
CGSize s1=CGSizeMake(100, 50);
NSSize s2=NSMakeSize(100, 50);

五 NSRect\CGRect

5.1 定义

1
2
3
4
struct CGRect {
CGPoint origin;
CGSize size;
};

5.2 使用

1
2
3
4
5
6
//NSRect\CGRect
CGRect r1=CGRectMake(0, 0, 100, 50);
NSRect r2=NSMakeRect(0, 0, 100, 50);
CGRect r3={{0,0},{100,50}};
CGRect r4={p1,s2};
NSLog(@"x=%f,y=%f,width=%f,height=%f",r1.origin.x,r1.origin.y,r1.size.width,r1.size.height);

六 转换

1
2
3
4
5
6
7
//转换
NSString *strfromPoint=NSStringFromPoint(p1);
NSLog(@"%@",strfromPoint);
NSString * strFromSize=NSStringFromSize(s1);
NSLog(@"%@",strFromSize);
NSString *strFromRect=NSStringFromRect(r1);
NSLog(@"%@",strFromRect);

七 比较

1
2
3
4
5
//比较
BOOL isEqual= CGPointEqualToPoint(CGPointZero, CGPointMake(0,0));
NSLog(@"%d",isEqual);
BOOL isContain= CGRectContainsPoint(CGRectMake(0,0, 100, 100), CGPointMake(50,50));
NSLog(@"%d",isContain);