Dependency: NSRange: NSObjCRuntime: Foundation
Dependency: CGPoint: CoreGraphics
Dependency: NSSize: CoreGraphics
Dependency: NSRect: CoreGraphics
Objective-C provides us with five common structures. The defined variables can exist on the stack and on the heap.
Namely:
- Range: NSRange,
- Point (NSPoint)
- Size (NSsize)
- Area (NSRect)
- Vector (CGVector);
NSRange
NSRange is used to represent the range of related things.
Structure prototype:
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
NSRange: (used to store words or statements, where they are)
location: starting position,
length: number of elements.
A shortcut function NSMakeRange() provided by cocoa for NSRange;
The advantage of using NSMakeRange() is that it can be used anywhere a function can be used, such as passing it as a parameter in a method call.
NSRange is a structure, not a class. The defined variables do not need to be added*
Example:
//NSRange /* typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; */ //1. NSRange creation NSRange range1; range1.location = 18; range1.length = 34; NSLog(@"range1.length = %zi",range1.length); NSLog(@"range1.location = %zi",range1.location); //Note L: printing can use% zi or% lu //2. Fast creation of NSRange NSRange range2 = NSMakeRange(8, 10); NSLog(@"range2.length = %zi",range2.length); NSLog(@"range2.location = %zi",range2.location); //or NSRange range3 = {17,4}; NSLog(@"range3.length = %zi",range3.length); NSLog(@"range3.location = %zi",range3.location); //3. NSStringFromRange converts the above structure to string type and prints it out //%@Is an OC object, range represents a structure, and str is an OC object NSString* str1 = NSStringFromRange(range2); NSLog(@"str1 = %@",str1);
Execution result:
2019-05-23 14:57:15.429762+0800 MGNSRange...[2563:568535] range1.length = 34
2019-05-23 14:57:15.429806+0800 MGNSRange...[2563:568535] range1.location = 18
2019-05-23 14:57:15.429820+0800 MGNSRange...[2563:568535] range2.length = 10
2019-05-23 14:57:15.429839+0800 MGNSRange...[2563:568535] range2.location = 8
2019-05-23 14:57:15.429855+0800 MGNSRange...[2563:568535] range3.length = 4
2019-05-23 14:57:15.429871+0800 MGNSRange...[2563:568535] range3.location = 17
2019-05-23 14:57:15.430104+0800 MGNSRange...[2563:568535] str1 = {8, 10}
CGPoint
CGPoint represents a point on a plane.
Structure prototype:
struct
CGPoint {
CGFloat x;
CGFloat y;
};
Note: x,y is the value of float type.
Example:
//CGPoint /* CGPoint { CGFloat x; CGFloat y; }; */ //1.CGPoint creation CGPoint point1; point1.x = 10; point1.y = 20; NSLog(@"point1.x = %f|point1.y = %f",point1.x,point1.y); //2.CGPoint quick creation CGPoint point2 = CGPointMake(10.01, 20.02); NSLog(@"point2.x = %f|point2.y = %f",point2.x,point2.y); //or CGPoint point3 = {10.01, 20.02}; NSLog(@"point3.x = %f|point3.y = %f",point3.x,point3.y);
Execution result:
2019-05-23 14:57:15.430154+0800 MGNSRange...[2563:568535] point1.x = 10.000000|point1.y = 20.000000
2019-05-23 14:57:15.430171+0800 MGNSRange...[2563:568535] point2.x = 10.010000|point2.y = 20.020000
2019-05-23 14:57:15.430185+0800 MGNSRange...[2563:568535] point3.x = 10.010000|point3.y = 20.020000
CGSize
CGSize is used to store length and width. Size.
Structure prototype:
struct CGSize {
CGFloat width;
CGFloat height;
};
Example:
//CGSize /* struct CGSize { CGFloat width; CGFloat height; }; */ // CGSize create CGSize size1; size1.width = 15; size1.height = 20; NSLog(@"size1.width = %f| size1.height = %f",size1.width,size1.height); //Quick creation CGSize size2 = CGSizeMake(20 , 30); NSLog(@"size2.width = %f| size2.height = %f",size2.width,size2.height); //or CGSize size3 = {15,20}; NSLog(@"size3.width = %f| size3.height = %f",size3.width,size3.height);
Execution result:
2019-05-23 14:57:15.430198+0800 MGNSRange...[2563:568535] size1.width = 15.000000| size1.height = 20.000000
2019-05-23 14:57:15.430210+0800 MGNSRange...[2563:568535] size2.width = 20.000000| size2.height = 30.000000
2019-05-23 14:57:15.430223+0800 MGNSRange...[2563:568535] size3.width = 15.000000| size3.height = 20.000000
NSRect
NSRect is used to represent a region.
Structure prototype:
struct CGRect {
CGPoint origin;
CGSize size;
};
Example:
//CGRect /* struct CGRect { CGPoint origin; CGSize size; }; CGPoint { CGFloat x; CGFloat y; }; struct CGSize { CGFloat width; CGFloat height; }; */ // CGRect creation CGRect rect1; rect1.origin.x = 15; rect1.origin.y = 20; rect1.size.width=8; rect1.size.height=15; NSLog(@"rect1.origin.x = %f|rect1.origin.y = %f|rect1.size.width = %f|rect1.size.height = %f",rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height); // Quick creation CGRect rect2 = CGRectMake(15,20,15,20); NSLog(@"rect2.origin.x = %f|rect2.origin.y = %f|rect2.size.width = %f|rect2.size.height = %f",rect2.origin.x,rect2.origin.y,rect2.size.width,rect2.size.height); CGRect rect3 = {15,20,8,15}; NSLog(@"rect3.origin.x = %f|rect3.origin.y = %f|rect3.size.width = %f|rect3.size.height = %f",rect3.origin.x,rect3.origin.y,rect3.size.width,rect3.size.height);
Execution result:
2019-05-23 14:57:15.430270+0800 MGNSRange...[2563:568535] rect1.origin.x = 15.000000|rect1.origin.y = 20.000000|rect1.size.width = 8.000000|rect1.size.height = 15.000000
2019-05-23 14:57:15.430285+0800 MGNSRange...[2563:568535] rect2.origin.x = 15.000000|rect2.origin.y = 20.000000|rect2.size.width = 15.000000|rect2.size.height = 20.000000
2019-05-23 14:57:15.430309+0800 MGNSRange...[2563:568535] rect3.origin.x = 15.000000|rect3.origin.y = 20.000000|rect3.size.width = 8.000000|rect3.size.height = 15.000000
CGVector
CGVector vector
Structure prototype:
struct CGVector { CGFloat dx; CGFloat dy; };
Example:
//CGVector /* struct CGVector { CGFloat dx; CGFloat dy; }; */ // CGVector creation CGVector vector1; vector1.dx = 10; vector1.dy = 20; NSLog(@"vector1.x = %f|vector1.y = %f",vector1.dx,vector1.dy); //2. Create quickly CGVector vector2 = CGVectorMake(10.01, 20.02); NSLog(@"vector2.dx = %f|vector2.dy = %f",vector2.dx,vector2.dy); //or CGVector vector3 = {10.01, 20.02}; NSLog(@"vector3.dx = %f|vector3.dy = %f",vector3.dx,vector3.dy);
Execution result:
//CGVector
/*
struct CGVector {
CGFloat dx;
CGFloat dy;
};
*/
// CGVector creation
CGVector vector1;
vector1.dx = 10;
vector1.dy = 20;
NSLog(@"vector1.x = %f|vector1.y = %f",vector1.dx,vector1.dy); //2. Create quickly CGVector vector2 = CGVectorMake(10.01, 20.02); NSLog(@"vector2.dx = %f|vector2.dy = %f",vector2.dx,vector2.dy); //or CGVector vector3 = {10.01, 20.02}; NSLog(@"vector3.dx = %f|vector3.dy = %f",vector3.dx,vector3.dy);
Note:
1. Zero
CGPointZero;
CGSizeZero;
CGRectZero;
2. Judge whether it is equal
The prerequisite for using the CGRectEqualToRect function is to add the CoreGraphics framework
//Judge whether it is equal BOOL isYES1 = CGRectEqualToRect(CGRectMake(0, 0, 80, 80), CGRectMake(0, 0, 80, 80)); //Other similarities //CGSizeEqualToSize(CGSize size1, CGSize size2); //CGPointEqualToPoint(CGPoint point1, CGPoint point2); //Determine whether the left side contains the right side BOOL isYES2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(60, 45)); NSLog(@"isYES1 = %i|isYES2 = %i",isYES1,isYES2);
results of enforcement
2019-05-23 14:57:15.430366+0800 MGNSRange...[2563:568535] isYES1 = 1|isYES2 = 1