Dark Horse programmer--oc Language Basic Grammar Knowledge Foundation

Source: Internet
Author: User

1. Structure: Nsrange, Nspoint (Cgpoint), Nssize (cgsize), Nsrect (CGRect)

Nsrange: Represents a range with two member variables: location, length

For example: @ "I love You" to find out the range of love

int main ()

{

Nsrange r1= nsmakerange{2,4};

return 0;

}

Example: Find out the range of a string in str

NSString *str = @ "I love You";

Nsranger r2 = [str rangeofstring: @ "VE"];

NSLog (@ "loc =%ld,len=%ld", r2.location,r2.length);

Point, Size, rect, and range are all similar usages

Cgpoint P1 = Cgpointmake (20,20);//Represents a coordinate

cgsize S1 = cgsizemake (200,100);//indicates length and width

CGRect r1 = CGRectMake (0,0,100,122);//Represents a block in a coordinate

2. String: NSString nsmutablestring

NSString: Immutable string, once assigned, cannot change the object's contents in the future.

Nsmutablestring: variable string, want to increase later, want to delete the deletion

For example:

int main ()

{

nsmutablestring *s1 = [nsmutablestring stringWithFormat: @ "My age is 10"] '

Stitch it in the back.

[S1 appendString: @ "11 12 13"]; The result is my 10 11 12 13

Get the scope of IS

Nsrange range = [S1 rangeofstring: @ "is"];

[S1 deletecharactersinrange:range];//Delete the contents of range

NSLog (@ "s1=%@", S1); Result my age 10 11 12 13

}

3. Array: Nsarray Nsmutablearray

Nsarray: Non-variable group

1. Quickly create a Nsarray object

Nsarray *array = @[@ "Jack" @ "Rose" @ "Jim"];

2. Quick access to elements in an array

NSLog (@ "%@", array[2]);//The compiler encounters this line of code, which is automatically translated as: NSLog (@ "%@", [Array object atindex:2]);

3. Fast traversal of arrays

ID obj represents each element in an array

for (id obj in Array)

{

Find the position of the obj element in the array

Nsuinteger i = [array indexofobject:obj];

NSLog (@ "%d-%d", i,obj);

}

4. Using block traversal

Each time a block is traversed, it is called once, and the current element and index position are passed as parameters to the block

[Array Enumerateobjectsusingblock: ^ (id obj,nsuinteger idx, BOOL *p)

{

NSLog (@ "%ld-%ld", idx,obj);

if (idx = = 1)

{

Stop traversal

*stop = YES;

}

}

];

Nsmutablearray: Variable Array

Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "Jack" @ "Rose", nil];

adding elements

[Array Addobject:[[person alloc] init];

[Array addobject:@ "Jake"];

Delete all elements

[Array removeallobjects];

Delete an Established object

[Array removeobject:@ "Jack"];

[Array removeobjectatindex:0];

/*

Note: @[@ "Jack" @ "Rose"]: can only be used to create immutable variable groups Nsarray

Error wording:

Nsmutablearray *array = @[@ "Jack" @ "Rose"];

[Array AddObject: @ "Jim"];

Nsmutablearray and AddObject are mutable arrays and @[@ "Jack" @ "Rose" is an immutable group

*/

Comparison of 4.NSSet and Nsarray

1. Common denominator: ① are all collections and can hold multiple OC objects

② can only exist OC objects and cannot hold non-OC objects

The ③ itself is immutable and has a mutable subclass

2. Different points: Nsarray ordered Nsset disorder

5: The Application of the dictionary

1. Dictionary: Nsdictionary nsmutabledictionary

1.NSDictionary

Key--value

Index--Content

It's all stored in key-value pairs.

Nsdictionary *dict = @

{

@ "name": @ "Jack",

@ "Address": @ "Beijing",

@ "QQ": @ "123543", nil

}

ID obj = [dict objectforkey: @ "QQ"];

NSLog (@ "%@", obj);//print Result: 123543

ID obj = dict[@ "QQ"];

The number of key-value pairs returned

NSLog (@ "%ld", Obj.count);

2.NSMutableDictionary

int main ()

{

Nsmutabledictionary *dict = [Nsmutabledictionary dictionary];

To add a key-value pair

[Dict setobject:@ "Jake" forkey:@ "name"];

[Dict setobject:@ "Address" forkey:@ "Beijing"];

[Dict setobject:@ "Jack" forkey:@ "name"];//the same key, the back of which will overwrite the previous one, then the 2 key-value pairs now

Removing key-value pairs

[Dict removeobjectforkey @ "name"];

NSString *str = dict[@ "name"];

NSLog (@ "%@", str);//print Result: Rose

}

Using block traversal

[Dict Enumeratekeysandobjectsusingblock:

^ (id key,id obj,bool *stop)

{

NSLog (@ "%@--%@", key,obj);

}

];

Dark Horse programmer--oc Language Basic Grammar Knowledge Foundation

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.