Structures commonly used in Objective-C: nsange, NSPoint, NSSize (CGSize), NSRect

Source: Internet
Author: User

Structures commonly used in Objective-C: nsange, NSPoint, NSSize (CGSize), NSRect

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









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.