Dark Horse Programmer -- OC struct in Foundation, foundationoc
OC struct
Nsange
NSPoint \ CGPoint
NSSize \ CGSize
NSRect \ CGRect
1. nsange: The range (Position, length) of Characters in the string)
Use typedef to define the nsange struct type:
Typedef struct _ nsange {
NSUInteger location;
NSUInteger length;
} Nsange;
Example:
NSString * str = @ "I love you ";
Nsange range1 = NSMakeRange (2, 4); // then, r indicates the love range.
Nsange range2 = [str rangeOfString: @ "love"]; // defines the range variable of the nsange struct type and stores the position and length of the string @ "love" in range2.
NSLog (@ "loc = % ld length = % ld", range2.location, range2.length); // print the location and length of love, and the result is loc = 2 length = 4.
If the range of a string cannot be found, length = 0, location = NSNotFound =-1.
2. NSPoint \ CGPoint:Determine the coordinates of a point in the plane
Typedef struct CGPoint {
CGFloat x;
CGFloat y;
} CGPoint;
Typedef CGPoint NSPoint;
Example:
CGPoint p1 = NSMakePoint (10, 10); // the x and y values of Point p1 are both 10.
NSPoint p2 = CGPointMake (5, 6); // This method is commonly used; point p2.x = 5, p2.y = 6.
The struct can be converted into strings to facilitate viewing the attributes of the struct.
NSString * str = NSStringFromPoint (p1); // convert Point p1 into a string and save it in str
NSLog (@ "% @", str); // enter the string {10, 10}
In the following two cases, you must run the program in the CoreGraphics framework.
1. Compare whether the two points are the same
BOOL a = CGPointEqualToPoint (CGPointMake (10, 10), CGPointMake (10, 10 ));
2. The following indicates the coordinate origin.
CGPointZero is equivalent to CGPointMake (0, 0)
3. NSSize \ CGSizeDescribes the size (width and height) of a UI element)
Struct CGSize {
CGFloat width;
CGFloat height;
};
Typedef struct CGSize;
Typedef CGSize NSSize;
Example:
NSSize s1 = NSMakeSize (15, 20 );
CGSize s2 = NSMakeSize (45, 23 );
NSSize s3 = CGSizeMake (20, 20 );
NSString * str1 = NSStringFromSize (s1 );
NSLog (@ "% @", str1 );
The CGSizeEqualToSize function to be called must also be run in CoreGraphics framework.
NSSize s1 = NSMakeSize (15, 20 );
CGSize s2 = NSMakeSize (45, 23 );
BOOL B = CGSizeEqualToSize (s1, s2 );
// Or write the line code BOOL B = CGSizeEqualToSize (CGSizeMake (), CGSizeMake ));
NSLog (@ "% d", B );
4. NSRect \ CGRect:Determine a rectangle
Struct CGRect {
CGPoint origin;
CGSize size;
};
Typedef struct CGRect;
Typedef CGRect NSRect;
Example: CGRect defines variables and initialization methods as follows.
CGRect r = CGRectMake (2, 2, 50, 12 );
CGRect r1 = CGRectMake (3, 2, 25, 12 );
CGRect r2 = {p1, s1 };
CGRect r3 = }};
CGRect r3 = {CGPointZero, CGSizeMake (50,12 )};
You can also convert this struct into a string.
NSString * str2 = NSStringFromRect (r );
NSLog (@ "% @", str2 );
Use the following function to compare whether two rectangles are equal
CGRectEqualToRect (r, r1 );
You can use the following function to determine whether a vertex is in a rectangle.
BOOL b1 = CGRectContainsCGPoint (CGRectMake (,), CGPointMake ));