Dark Horse Program Ape ———— OC in foundation frame structure and string

Source: Internet
Author: User


------<a href= "http://www.itheima.com" target= "blank" >java train, Android train, iOS train,. NET Train </a>, we look forward to communicating with you!

-------

The foundation framework includes many data types that are often used in development, for example. struct, string. collection classes, and so on. Is the basis for other iOS frameworks.

Assuming that you want to use the data type of the foundation framework, just include its

The header file is available. namely #import<foundation/foundation.h>


Structures commonly used in foundation:

In the foundation framework, there are four types of structures commonly used:

Nsrange

Nspoint\cgpoint

Nssize\cgsize

Nsrect\cgrect


Nsrange: Indication Range (location length)

Definition of Nsrange: position and length

typedef struct _NSRANGE {     nsuinteger location;     Nsuinteger length;     } Nsrange;
Basic use of Nsrange:
@ "I love OC"  //love range    Nsrange r1 = {2,4};  Readability is relatively poor, in development generally do not    nsrange r2 = {. Location = 2,. length = 4};  No        //actual development is most often used with the function that comes with the foundation. For example the following:    nsrange r3 = Nsmakerange (2, 4);/        /requires mastering//String creation the quickest way    nsstring *str = @ "I love oc";       Rangeofstring find the range of a string, find the range of love    //Suppose not found, length= 0,location = Nsnotfound = =-1    nsrange range = [str Rangeofstring:@ "Love"];    Print out    NSLog (@ "loc =%ld,length =%ld", range.location,range.length);

nspoint\cgpoint: (indicates coordinates)

Nspoint and Cgpoint are the same. Frequently used cgpoint in development

typedef cgpoint Nspoint;
struct Cgpoint {  cgfloat x;  CGFloat y;      The cgfloat type. The actual is double};typedef struct cgpoint cgpoint;
Use of Nspoint\cgpoint:

    Cgpoint is often used in development, because he is cross-platform    //create struct variable    nspoint p1 = Cgpointmake (ten);    Most often used    cgpoint P2 =  nsmakepoint (10, 10);

Nssize\cgsize: (Represents the dimensions, width, and height of UI elements)

The method of use is basically the same as Nspoint

typedef cgsize Nssize;
struct Cgsize {  cgfloat width;  CGFloat height;}; typedef struct CGSIZE cgsize;

Basic use of Nssize\cgsize:

Create struct variable   nssize s1 =  Cgsizemake (+);   Nssize s2 =  nsmakesize (+);   Cgsize s3 =  nsmakesize (200, 100);

Nsrect\cgrect: (Represents the location and size of a UI element Cgpoint cgsize)

typedef cgrect Nsrect;
struct CGRect {  cgpoint origin;  Cgsize size;}; typedef struct CGRECT CGRect;

Basic use:

Create struct variable    //mode one    cgrect  r1 = CGRectMake (0, 0,.);    Mode two    CGRect r2 = {{0,0},{50,100}};    Way three    CGRect r3 = {p1, s2};        CGRect R4 = {Cgpointzero,cgsizemake (+)};    //cgpointzero ==cgpointmake (0, 0)    are often used in development, as indicated by the origin. //    General use://    cgrect myrect (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;//    }    //development is often the first to turn the structure into a string before printing
    The way the struct is converted to a string. Requirements Master        //nsstring *str =  nsstringfrompoint (p1);  Convert the coordinates into a string        //nsstring *str = nsstringfromsize (S3);  Bar size converted to string        nsstring *str = nsstringfromrect (r1);   Convert position and dimension to string form to print
     NSLog (@ "%@", str);
       Try printing, this way is more troublesome. Not used in development   //NSLog (@ "x =%f,y =%f, width =%f,height =%f", r1.origin.x,r1.origin.y,r1.size.width,r1.size.height);


Some of the frequently used struct functions in the Foundation framework:

The premise of using these functions is to join the Coregraphics framework

The position of the two points is the same (x, y)  BOOL b = <span style= "color: #ff0000;" > Cgpointequaltopoint (Cgpointmake (Ten), Cgpointmake (Ten));    Is the same as two areas    of the same] Cgrectequaltorect (< #CGRect Rect1#>, < #CGRect rect2#>)    //Comparison of two sizes is the same   Cgsizeequaltosize (< #CGSize Size1#>, < #CGSize size2#>)    //Infer whether this point is within the range    //x (50,100)  y (40,90 )    BOOL b2 = cgrectcontainspoint (CGRectMake (n, N, 1010, N), Cgpointmake (());        NSLog (@ "%d", B2);


NSString:

NSString: Immutable string

Nsmutablestring: Variable string

How strings are created:

 The creation of the string nsstring *s1 = @ "Jack";  NSString *s2 = [[NSString alloc]initwithstring:@ "Jack"];        Almost no one used nsstring *s3 = [[NSString alloc] initwithformat:@ "Age is%d", 10];        C String----OC string NSString *s4 = [[NSString alloc]initwithutf8string: "Jack"];        OC string--C string const char *cs = [S4 utf8string]; Nsutf8stringencoding can be used in Chinese to use such a code//read the contents of the file NSString *s5 = [[NSString alloc]initwithcontentsoffile:@]/users/        mymac/desktop/test/Memory Management/summary. M "Encoding:nsutf8stringencoding Error:nil];        NSLog (@ "\n%@", S5); URL: Resource Path//protocol header://Path//(local file) file:////ftp:////http://weibo.com/a.png//nsurl *url = [[Nsurl alloc]        initwithstring:@ "file:///Users/mymac/Desktop/test/memory Management/summary. M"];        Nsurl *url = [Nsurl fileurlwithpath:@ "//users/mymac/desktop/test/memory Management/summary. M"];        NSString *S6 = [[NSString alloc]initwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil];            NSLog (@ "s6 = \n%@", S6);    /* When creating strings, there are usually some class methods that typically have a class method paired with an object method, which is often used in the development of a class method.          Very few object methods are used to save time [Nsurl urlwithstring: (NSString *)];          [NSString stringwithformat:@ ""]; [NSString stringwithcontentsoffile:<# (NSString *) #> encoding:<# (nsstringencoding) #> error:<# (     Nserror *__autoreleasing *) #>]; */

The way the string is written to the file:

The method of writing the string to the file   [@ "Jack\njack" writetofile:@ "/users/mymac/desktop/test/memory Management/summary. M" Atomically:yes encoding: Nsutf8stringencoding Error:nil];        NSString *str = @ "Rweudjkqsdjiw";        Nsurl *url = [Nsurl fileurlwithpath:@ "/users/mymac/desktop/test/memory Management/summary. M"];        [Str writetofile:url atomically:yes encoding:nsutf8stringencoding Error:nil];

Use of variable strings:

The creation of a mutable string    nsmutablestring *s1 = [nsmutablestring stringwithformat:@ "Age is ten"];        Stitching the contents to the back of the S1    [S1  appendformat:@ "];        Get the IS scope    nsrange range = [S1 rangeofstring:@ "is"];        [S1 Deletecharactersinrange:range];  Delete        NSString *s2 = [NSString stringwithformat:@ "Age is ten"];        NSLog (@ "S1 =%@,s2 =%@", s1,s2);




------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS train,. NET Train </a>, we look forward to communicating with you! -------

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Dark Horse Program Ape ———— OC in foundation frame structure and string

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.