Blackhorse programmer _ objective-C Foundation notes (2)

Source: Internet
Author: User
Nsarray

 

Create nsarray

Nsarray * array = [nsarray arraywithobject: @ "Jack"] create a single element

 

Nsarray * array3 = [nsarray arraywithobjects: @ "Jack", @ "Rose", nil] create multiple elements

 

Quickly create an nsarray object

Nsarray * array4 = @ [@ "Jack", @ "Rose", @ "4324324"]

 

[Array. Count] calculates the number of array elements

 

Array3 [0]

[Array3 objectatindex: 1] elements accessing the Array

 

Note: The OC array cannot store nil values, and nil is the end mark of the array.

The OC array can only store OC objects, but cannot store non-OC object types, such as int, struct, and enum.

Nsarray * array = [nsarray array] This array is always an empty array

 

Traverse Arrays

 

Method 1,

Person *p = [[Person alloc] init];        NSArray *array = @[p, @"rose", @"jack"];           for (int i = 0; i<array.count; i++)       {            NSLog(@"%@", array[i]);       }

 

 

Method 2,

 

Id OBJ represents each element in the array int I = 0; For (id obj in array) {// locate the position of the OBJ element in the array nsuinteger I = [array indexofobject: OBJ]; nslog (@ "% LD-% @", I, OBJ); // I ++; if (I = 1) {break ;}}

 

 

Method 3,

 

 

[Array enumerateobjectsusingblock: // each time an element is traversed, a block is called. // The current element and index position are passed as parameters to the block ^ (id obj, nsuinteger idx, bool * Stop) {nslog (@ "% LD-% @", idx, OBJ); If (idx = 0) {// stop traversal * Stop = yes;}];

 

 

Nsmutablearray

 

Create an array

Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "Rose", @ "Jim", nil]

 

Add Element

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

[Array addobject: @ "Jack"];

 

Delete Element

[Array removeallobjects]

Deletes a specified object.

[Array removeobject: @ "Jack"];

[Array removeobjectatindex: 0];

 

Nsset

 

Create

Nsset * s = [nsset set]

Nsset * S2 = [nsset setwithobjects: @ "Jack", @ "Rose", @ "jack2", @ "jack3", nil]

 

Randomly extract an element

Nsstring * STR = [S2 anyobject];

Nslog (@ "% @", STR );

 

Nsmutableset

 

Nsmutableset * s = [nsmutableset];

Add Element

[S addobject: @ "hack"];

Delete Element

[S removeobject: <# (ID) #>];

 

 

Comparison between nsset and nsarray

1. Commonalities

1) All are collections and can store multiple OC objects.

2) Only OC objects can be stored, not non-OC object types (basic data types: int, Char, float, struct, enumeration)

3) They are both immutable and each has a mutable subclass.

 

2 Differences

Nsarray has sequence, nsset has no sequence

 

Nsdictionary

 

A dictionary is composed of an index (key) and text content (value)

 

Create an unchangeable dictionary

 

Method 1,

Nsdictionary * dict = [nsdictionary dictionarywithobject: @ "Jack" forkey: @ "name"];

Method 2,

Nsarray * keys = @ [@ "name", @ "Address"];

Nsarray * objects = @ [@ "Jack", @ "Beijing"];

Nsdictionary * dict = [nsdictionary dictionarywithobjects: Objects forkeys: Keys];

Method 3,

Nsdictionary * dict = [nsdictionary dictionarywithobjectsandkeys:

@ "Jack", @ "name ",

@ "Beijing", @ "Address ",

@ "32423434", @ "QQ", nil];

Quickly create a dictionary

Nsdictionary * dict =@ {@ "name": @ "Jack", @ "Address": @ "Beijing "};

 

Retrieve a dictionary object

Id OBJ = [dict objectforkey: @ "name"]

 

Quickly retrieve a dictionary object

Id OBJ = dict [@ "name"]

 

Variable dictionary nsmutabledictionary

 

Nsmutabledictionary * dict = [nsmutabledictionary dictionary]

 

Add a key-Value Pair

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

 

Remove a key-Value Pair

[Dict removeobjectforkey: @ "name"];

 

Traverse dictionary

 

Method 1,

 

  NSArray *keys = [dict allKeys];            for (int i = 0; i<dict.count; i++)        {            NSString *key = keys[i];            NSString *object = dict[key];                    NSLog(@"%@ = %@", key, object);        }    

 

Method 2,

 

 

 [dict enumerateKeysAndObjectsUsingBlock:     ^(id key, id obj, BOOL *stop) {         NSLog(@"%@ - %@", key, obj);                  // *stop = YES;     }];

Blackhorse programmer _ objective-C Foundation notes (2)

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.