iOS 结构体

版权声明:本文为博主原创,如需转载请注明出处。

结构体

NSRange(location length)

1
2
3
4
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
1
2
3
NSRange r1 = {2, 4}; // 不用
NSRange r2 = {.location = 2, .length = 4};// 不用
NSRange r3 = NSMakeRange(2, 4); // 掌握
1
2
3
4
5
6
NSString *str = @"i love oc";

// 查找某个字符串在str中的范围
// 如果找不到,length=0,location=NSNotFound==-1
NSRange range = [str rangeOfString:@"java"];
NSLog(@"loc = %ld, length=%ld", range.location, range.length);

NSPoint\CGPoint, NSSize\CGSize, NSRect\CGRect (CGPint CGSize)

1
2
3
4
5
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
1
2
3
4
5
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
1
2
3
4
5
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
CGPoint p1 = NSMakePoint(10, 10);
NSPoint p2 = CGPointMake(20, 20);// 最常用

NSSize s1 = CGSizeMake(100, 50);
NSSize s2 = NSMakeSize(100, 50);
CGSize s3 = NSMakeSize(200, 60);

CGRect r1 = CGRectMake(0, 0, 100, 50);

CGRect r2 = { {0, 0}, {100, 90}};

CGRect r3 = {p1, s2};

// 使用CGPointZero等的前提是添加CoreGraphics框架
CGRect r4 = {CGPointZero, CGSizeMake(100, 90)};


// CGSizeZero
// CGRectZero

// 表示原点
// CGPointZero == CGPointMake(0, 0)


// 将结构体转为字符串
//NSString *str = NSStringFromPoint(p1);

//NSString *str = NSStringFromSize(s3);

NSString *str = NSStringFromRect(r1);

NSLog(@"%@", str);

使用这些CGPointEqualToPoint、CGRectContainsPoint等函数的前提是添加CoreGraphics框架

1
2
3
4
5
6
7
8
9
10
11
12
// NextStep  Foundation 

// 比较两个点是否相同(x、y)
BOOL b = CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
//CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
//CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>)


// x (50, 150) y (40 , 90)
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45));

NSLog(@"%d", b2);

新博客文章地址:
CSDN文章地址:

文章目录
  1. 1. 结构体
    1. 1.1. NSRange(location length)
    2. 1.2. NSPoint\CGPoint, NSSize\CGSize, NSRect\CGRect (CGPint CGSize)