Structures commonly used in objective-C: nsange, nspoint, nssize (cgsize), nsrect
1 nsange
The prototype of nsange is
typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange;
Nsmakerange Functions
Ns_inlinez is the inline function typedef nsange * nsangepointer; ns_inline nsange nsmakerange (nsuinteger Loc, nsuinteger Len) {nsange R; R. Location = LOC; R. Length = Len; return r ;}
Usage
// The Value of "nsange range" indicates the range of "nsange range" and "range. location = 18; range. length = 34; nslog (@ "location is % zi", range. location); nslog (@ "length is % zi", range. length); // quickly create range = nsmakerange (8, 10); nslog (@ "location is % zi", range. location); nslog (@ "length is % zi", range. length); // nsstringfromrange converts the above struct to the string type and prints the nsstring * str1 = nsstringfromrange (range); // % @ is an OC object, range represents a struct, and STR is an OC object nslog (@ "Rang is % @", str1 );
2 nspoint
Nspoint prototype
struct CGPoint { CGFloat x; CGFloat y; };
Nsmakepoint Function
NS_INLINE NSPoint NSMakePoint(CGFloat x, CGFloat y) { NSPoint p; p.x = x; p.y = y; return p; }
Cgpointmake Function
CGPointMake(CGFloat x, CGFloat y) { CGPoint p; p.x = x; p.y = y; return p; }
Usage
// Nspoint indicates the position nspoint point; // assign a value to the point in the struct. X = 10; point. y = 10; // quickly create a vertex point = nsmakepoint (10, 18); // a common function is cgpointmake's Function Point = cgpointmake (29, 78 ); nsstring * str2 = nsstringfrompoint (point); nslog (@ "point is % @", str2 );
3 cgsize
Cgsize prototype
struct CGSize { CGFloat width; CGFloat height; };
Nsmakesize Function
NS_INLINE NSSize NSMakeSize(CGFloat w, CGFloat h) { NSSize s; s.width = w; s.height = h; return s; }
Cgsizemake Function
CGSizeMake(CGFloat width, CGFloat height) { CGSize size; size.width = width; size.height = height; return size; }
Usage
NSSize size; size.width = 100; size.height = 12; size = NSMakeSize(12, 12); size = CGSizeMake(11, 11); NSString* str3 = NSStringFromSize(size); NSLog(@"%@",str3);
4 cgrect
Cgrect prototype
struct CGRect { CGPoint origin; CGSize size; };
Cgrectmake Function
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height) { CGRect rect; rect.origin.x = x; rect.origin.y = y; rect.size.width = width; rect.size.height = height; return rect; }
Nsmakerect Function
NS_INLINE NSRect NSMakeRect(CGFloat x, CGFloat y, CGFloat w, CGFloat h) { NSRect r; r.origin.x = x; r.origin.y = y; r.size.width = w; r.size.height = h; return r; }
Usage
// Contains both the size and position of nsrect rect; rect. origin. X = 12; rect. origin. y = 14; rect. size. width = 12; rect. size. height = 15; // quick creation method rect = cgrectmake (12, 12, 12, 12); rect = nsmakerect (11, 11, 11, 11 ); // print nsstring * str5 = nsstringfromrect (rect); nslog (@ "rect is % @", str5 );