Common structures in IOS-Foundation: ios-foundation structure

Source: Internet
Author: User

Common structures in IOS-Foundation: ios-foundation structure

Many common struct types are defined in Foundation to simplify our daily development.

Nsange (location length) is used to indicate the range.

NSPoint \ CGPoint (the latter is often used in development)

NSSize \ CGSize

NSRect \ CGRect (CGPint CGSize)

Rnsange prototype

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

This is the internal structure of the nsange struct. It has two members, one length and one position. You do not need to use the struct keyword like c, and the member data type is NSUInteger.

typedef unsigned long NSUInteger;

It is an alternative to the unsigned long integer type, and you will know that it is only a positive integer.

 

Use the nsange struct to indicate the range, for example:

// {1, 2, 3, 4, 5} // indicates the range of 3, 4, 5, location = 2, length = 3

Starts from 0 and starts from 2nd. The length is 3.

 

The range of love in the string. It starts from 0 and counts 2nd positions. The length is 4.

@"i love oc";

 

The following two statements are not recommended (the former is too readable and the latter is too lengthy)

// Nsange r1 = {2, 4}; // No // nsange r2 = {. location = 2,. length = 4}; // No

The above two statements are not used.

Nsange r3 = NSMakeRange (2, 4); // master

 

A frequently used method for searching string locations in projects

NSString * str = @ "I love oc"; // it is a common and important method to find the range of a string in str! // If not found, length = 0, location = NSNotFound =-1 nsange range = [str rangeOfString: @ "java"]; NSLog (@ "loc = % ld, length = % ld ", range. location, range. length );

Because location is of the unsigned long type, the-1 print will be a very large positive number. Of course, if the highest bit is expressed as a negative number, that is, NSNotFound is printed as an (Signed) integer, it is-1.

 

NSPoint \ CGPoint

Vertex struct

struct CGPoint {  CGFloat x;  CGFloat y;};typedef struct CGPoint CGPoint;

Also:

typedef CGPoint NSPoint;typedef struct _NSPoint {    CGFloat x;    CGFloat y;} NSPoint;

View more:

 typedef CGFLOAT_TYPE CGFloat;

View more:

#define CGFLOAT_TYPE double

The essence is the double type.

Structures similar to the nsange

CGPoint p1 = NSMakePoint (10, 10); NSPoint p2 = CGPointMake (20, 20); // The most common

 

NSSize \ CGSize

It indicates the structure of height and width and Lenovo memory.

    NSSize s1 = CGSizeMake(100, 50);    NSSize s2 = NSMakeSize(100, 50);    CGSize s3 = NSMakeSize(200, 60);

 

CGRect indicates the rectangle size

    CGRect r1 = CGRectMake(0, 0, 100, 50);    CGRect r2 = { {0, 0}, {100, 90}};    CGRect r3 = {p1, s2};

We can see that there is a make method for creating common struct in the Foundation framework, which is also a common operation in the future;

There is also an NSStringFromXX Method for String Conversion, which facilitates debugging. As mentioned above, NSSize is actually CGSize, and NSRect is actually CGRect.

Note:

UI is the UI library of iOS, encapsulated in objective-c
NS is the basic database of objc.
CG, CF, and so on are the underlying C language Libraries
It can be understood that UIFont is a CGFont encapsulated by objc, which is more convenient to use.

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.