NSRange NSPoint NSSize of CGSize and NSRect instance analysis are commonly used structures in objective c

  • 2020-04-02 02:32:00
  • OfStack

This paper describes in detail the definition and usage of NSRange,NSPoint,NSSize(CGSize) and NSRect commonly used structures in objective-c with examples, as follows:

1, NSRange:

The prototype of NSRange is


typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;

NSMakeRange function:

NS_INLINEz is an inline function


typedef NSRange *NSRangePointer;

NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) {
NSRange r;
r.location = loc;
r.length = len;
return r;
}

Usage:


//NSRange means range
NSRange range;
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 structure to a string type and prints it out

NSString* str1 = NSStringFromRange(range);

//%@ is an OC object,range represents a structure, 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 refers to the location
NSPoint point;

//Assign values to the points in the structure
point.x = 10;
point.y = 10;

//Quick point creation
point = NSMakePoint(10, 18);

//The most common is the function that creates the point for CGPointMake
point = CGPointMake(29, 78);
NSString* str2 = NSStringFromPoint(point);
NSLog(@"point is %@",str2);

3, CGSize:

Prototype of CGSize:


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:

Prototype of CGRect:


struct CGRect {
CGPoint origin;
CGSize size;
};

Function of CGRectMake:


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;
}

Method of use


//It includes both size and location
NSRect rect;
rect.origin.x = 12;
rect.origin.y = 14;
rect.size.width = 12;
rect.size.height = 15;

//Quick create method
rect = CGRectMake(12, 12, 12, 12);
rect = NSMakeRect(11, 11, 11, 11);

//Convert to a string and print it out
NSString* str5 = NSStringFromRect(rect);
NSLog(@"rect is %@",str5);

Related articles: