iOS learning the next day (data types commonly used in OC)

Source: Internet
Author: User
Tags string to file

First, Cgpoint defines a point, and two parameters are X, Y, respectively

1>cgpoint p = {100,100};

2>cgpoint p;

p.x = 100;

P.Y = 100;

3>cgpoint p = cgpointmake (100,100);

Second, cgsize defines a size: two parameters are width, height

Three, CGRect defines a rectangle: Four parameters are origin.x, ORIGIN.Y, Size.width, Size.Height

Iv. Nsrange defines a range: Two parameters are: location, length

Five, static string NSString assignment statement:

1>nsstring * str = [NSString stringwithformat:@ "%d", 123];

2>nsstring * str = [NSString stringwithstring:@ "abc"];

3>nsstring * str = [[NSString alloc]initwithformat:@ "%d", 123];

4>nsstring * str = [NSString stringwithutf8string: "abc"];

Six, 1> all the strings into uppercase: str = [str uppercasestring];

2> all lowercase: str = [STR lowercasestring]

Seven: To find the string length:1> using [str lengthofbytesusingencoding:nsutf8stringencoding], the Chinese character length is 3;

2> when using [str length], the Chinese character length is 1;

Determine if there is a string in the string: Use Nsrrange range = [str rangeofstring:@ "AB"]; Then use the IF statement to Judge Range.location==nsnotfound to execute

Ix. Comparison of string contents: Using the If statement to determine [STR isequaltostring:@ "ABC"] to perform

Ten, the string is converted to a numeric value: int a = [str integervalue];//double a = [str doublevalue];

Xi. determine whether to start with what string: bool b = [str hasprefix:@ "A"];//end use [str hassuffix]

12. Extract substring:1> starting from somewhere: [str substringfromindex:2];//starting from 0, including the location

2> extracted to a location: [str substringtoindex:3];//starting from 0, excluding the location

3> extracts the middle of a few characters: Nsrange range = {n}; [STR substringwithrange:range];//can use this function to implement the reverse output of a string

13. Remove the spaces at both ends of the string: [str stringbytrimmingcharrctersinset:[nscharacterset whitespacecharacterset]];

14. Add a string after the string: [str stringbyappendingstring:@ "ABC"]

XV, modify the contents in the string: Nsrange R ={0,1}; [Str replacecharactersinrange:r withstring:@ "abc"];

16. Delete the content in the string: [str deletecharactersinrange:r];

17. Add content to the string: nsmutablestring * str = [nsmutablestring stringwithcapacity:100]; [String appendformat:@ "123"];

18. Write string to file: [str writetofile:@ "Abc.txt" Automically:no encoding:nsutf8stringencoding Error:nil];

19, static array of three methods of assignment:

1>nsarray * array = @[@ "1", @ "2", @ "3"];//Direct Assignment

2>nsarray * array = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", nil];

3>nsarray * array = [[Nsarray alloc]initwithobjects:@ "1", @ "2", @ "3", nil];

Two methods of traversal:

1> loop traversal for (int i=0;i<[array count];i++)

2> Fast Traversal for (NSString * str in array)

20. Dynamic array: Nsmutablearray *arr = [Nsmutablearray arraywithcapacity:20]

1> Add object: [arr addobject:[nsstring stringwithformat:@ "123"];

2> Delete object: [Arr removeobject:@ "2"];

3> Insert Object: [arr insertobject:@ "abc" atindex:0];

21, the contents of the collection can not be repeated:

Nsset * set = [Nsset setwithobject:@ "1", @ "2", @ "3", @ "2", nil];

Nsarray * arr = [set allobjects];

arr = [arr sortedarrayusingselector: @selector (compare:)];

22. Dynamic Collection:

Nsmutableset * set = [Nsmutableset setwithcapacity:20];

23. Static and Dynamic dictionary

Nsdictionary * Dict = [[Nsdictionary alloc] initwithobjectsandkeys:@ "Zhang San", @ "1", @ "John Doe", @ "2", Nil];//value,key

Nsmutabledictionary * Dict = [[Nsmutabledictionary alloc]initwithcapacity:20];

[Dict setobject:@ "Zhangsan" forkey:@ "1"];//add

[Dict setobject:@ "Lisi" forkey:@ "2"];

[Dict setobject:@ "Wangwu" forkey:@ "1"];

[Dict removeobjectforkey:@ "2"];//Delete

Nsarray * keys = [dict AllKeys];

For (NSString *str in keys)

{

NSLog (@ "key=%@,value=%@", Str,[dict Objectforkey:str]);

}

24, the array can only store object type, so before storing the first to use Nsvalue,nsnumber into object type

Cgpoint p1 = {100,100};

Nsvalue * v = [Nsvalue valuewithpoint:p1];

Nsarray *arr = [Nsarray Arraywithobjects:v,nil]

25, NSData:

Nsarray * array = [[Nsarray alloc]initwithobjects:@ "1111", @ "2222", @ "3333", @ "4444", nil];

Nsarray *array = [Nsarray arraywithcontentsoffile:@ "Array.plist"];//array writetofile: @ "array.plist" Atomically:NO]; Write to file, Nsset, nsdictionary

NSData * data = [NSData datawithcontentsoffile:@ "a.jpg"];//read out a.jpg file

[Data writetofile:@ "b.jpg" atomically:yes];//addressed to B.jpg file. Equivalent to copy function

iOS learning the next day (data types commonly used in OC)

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.