OC Language--Arrays & Dictionaries

Source: Internet
Author: User
Tags delete key

1. Arrays

1.1 Immutable Array (reading group only)

Note :the difference between OC arrays and C arrays:

-The method of creation is different;

-The OC array itself is also an object and is a collection of object addresses of any type;

-"OC-compatible array of C, which can be used to store objects;"

(1) creating an Array object

-"mode One": Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "twin", @ "three", @ "a", Nil];//nil as an OC array declaration, not in OC array element;-"mode two://Adopt class method to create OC array object Nsarray *array2 = [Nsarray arraywithobjects:@" one ", @" II ", @" four ", nil]; -"mode three (OBJECTIVE-C 2.0 new feature Support): Xcode4.6 later version support Nsarray *array3 = @[@" One One ", @" one ", @" three ", @" four "];

(2) array Elements

-"array element is an object address of any type

(3) iterating through an array

-"For loop traversal"

-"Fast enumeration method for in structure"

(4) Common methods

1 Common creation methods-(ID) initwithobjects: (ID) firstobj, ...; -(ID) Initwitharray: (Nsarray *) array;+ (ID) arraywithobjects: (ID) firstobj, ...; + (ID) Arraywitharray: (Nsarray *) array; 2 Gets the number of array elements-(Nsuinteger) count; 3 by index, get the corresponding element-(ID) Objectatindex: (Nsuinteger) index; 4 by object address, gets the index in the array-(Nsuinteger) Indexofobject: (ID) anobject; 5 in the array of judgments, the array contains the element anobject-(BOOL) Containsobject: (ID) anobject; 6 Gets the last element of the array-(ID) lastobject; 7 The contents of the array elements, separator by the string-(NSString *) componentsjoinedbystring: (NSString *) separator;

1. 2 variable Array

(1) Concept:

-"Object content can be changed

(2) The relationship between variable arrays and immutable groups

-"Nsmutablearray inherited from Nsarray

(3) manipulation of variable array objects for additions and deletions

(4) ordering of variable arrays

-"Use bubbling/selection method, two-layer for loop to sort variable array

-"How to use:

-(void) Sortusingselector: (SEL) comparator;

Note:

This sorting method has been implemented and requires us to provide a sorting criterion;

We need to convert a comparison method into the SEL selector type to pass in the above ordering method,

This comparison method needs to be written in the same class as the array element.

(5) Common methods

               1 Add array element-"append element-(void) AddObject: (ID) anobject;-" specify Index Insert element-(void) InsertObject: (ID) anobject atindex: (nsuinteger) index ;-"Append an array-(void) Addobjectsfromarray: (Nsarray *) otherarray;2 Delete-" Delete last element-(void) removelastobject;-"Deletes an element of the specified index-( void) Removeobjectatindex: (nsuinteger) index;-Delete all elements-(void) removeallobjects;-deletes all elements in the specified range-(void) Removeobject :(ID) anobject inrange: (nsrange) range;-Delete the specified element-(void) Removeobject: (id) anobject;-Delete all elements given by an array//given array Elements in Otherarray are all elements to be deleted-(void) Removeobjectsinarray: (Nsarray *) otherarray;3 Modify array//overwrite another array with one array     -(void) SetArray: (Nsarray *) Otherarray;4 replaces the element of the specified index  -(void) Replaceobjectatindex: (nsuinteger) index Withobject: (ID) Anobject;5 Interchange Array element  -(void) Exchangeobjectatindex: (Nsuinteger) idx1 Withobjectatindex: (nsuinteger) idx2;6 sort: Sort by sorting criteria//using selectors to sort by, selectors are function pointer objects, SEL is the type of function pointer object  -(void) Sortusingselector: (SEL) comparator;

eg. sample code

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {nsarray* Aarr        ay = [[Nsarray alloc]initwithobjects:@ "ni", @ "Wo", @ "Ta", nil];    for (Nsuinteger i = 0; i < [Aarray count]; i++) {NSLog (@ "%@", [Aarray objectatindex:i]);        }//mutable version of the array traversal nsmutablearray* muaarray = [[Nsmutablearray alloc]initwithobjects:@ "ni", @ "Wo", @ "Ta", nil];    for (Nsuinteger i = 0; i < [Aarray count]; i++) {NSLog (@ "%@", [Muaarray objectatindex:i]);      }//Add an array element [Muaarray addobject:@ "Nimen"];    Detects if the array contains a specific element//judgment array, array containing element AnObject//-(BOOL) Containsobject: (ID) anobject;    if ([Muaarray containsobject:@ "Nimen"]) {NSLog (@ "array contains this element");    }else{NSLog (@ "The object element not found");      } [Muaarray Removelastobject];      [Muaarray removeobjectatindex:0];    [Muaarray removeobject:@ "Wo"];    for (Nsuinteger i = 0; i < [Muaarray count]; i++) {NSLog (@ "%@", [Muaarray objectatindex:i]); }          }    return 0;} 

2. Dictionaries

2.1 Immutable Dictionaries

(1) Dictionary

-The dictionary is also a collection structure that functions like a dictionary tool in our reality

(2) Elements of a dictionary

-"A key value pair consisting of any type of object address

(3) Key-value pairs

-"Key value pairs are composed of key and value, and must be one by one corresponding

-"Keys in key-value pairs must be unique in the dictionary and cannot be duplicated

(4) how to find values

-"Find the corresponding value according to key in the value pair

(5) Traverse Dictionary

-"Use fast enumeration for in structure, traverse dictionary

(6) Common methods

                1 Creation Method-"instantiation Method  -(ID) Initwithobjectsandkeys: (ID) firstobject, ...;  -(ID) initwithdictionary: (Nsdictionary *) otherdictionary;  -(ID) initwithobjects: (Nsarray *) objects Forkeys: (Nsarray *) keys;-"class method creation  + (ID) Dictionarywithobjectsandkeys: (ID ) Firstobject, ...;  + (ID) dictionarywithdictionary: (Nsdictionary *) dict;  + (ID) dictionarywithobjects: (Nsarray *) objects Forkeys: (Nsarray *) Keys;2 get the number of key-value pairs  -(Nsuinteger) count;3 based on key Get the corresponding value  -(ID) Objectforkey: (ID) akey;4 get all keys in the dictionary  -(Nsarray *) Allkeys;5 Get the value is anobject all key  -( Nsarray *) Allkeysforobject: (ID) anobject;    6 get all the values in the dictionary  -(Nsarray *) allvalues;

2.2 Variable Dictionary

(1) Common methods

              1 Add-"Add entire dictionary  -(void) Addentriesfromdictionary: (nsdictionary *) otherdictionary;-" Increase key value to//key does not exist then increase, Key exists modify key corresponding value  -(void) SetObject: (ID) anobject forkey: (id <NSCopying>) akey;2 delete-"Delete key value pair based on key  -(void) Removeobjectforkey: (id) akey;-Delete all key-value pairs  -(void) removeallobjects;-the corresponding key-value pairs  -(void) based on the contents of the array. Removeobjectsforkeys: (Nsarray *) keyarray;3 Modify-"Modify entire dictionary  -(void) Setdictionary: (nsdictionary *) otherdictionary;-" Modifier key value pair//key does not exist then increase, key exists to modify key corresponding value  -(void) SetObject: (ID) anobject forkey: (id <NSCopying>) Akey;

eg. sample code

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {nsdictionary* Onedi    c = [[Nsdictionary alloc]initwithobjectsandkeys:@ "Beijing", @ "one", @ "Shanghai", @ "one", @ "GuangZhou", @ "three", nil];      NSLog (@ "%@", onedic); Create mutable Dictionary nsmutabledictionary* twodic = [[Nsmutabledictionary alloc]initwithobjectsandkeys:@]     Beijing ", @" one ", @" Shanghai ", @" one ", @" GuangZhou ", @" three ", nil];      NSLog (@ "%@", twodic);    Add a key-value couple [twodic setobject:@ "Shenzheng" forkey:@ "four"];          NSLog (@ "%@", twodic);          nsarray* Keyarray = [Nsarray arraywithobjects:@ "," @ "four", nil];    [Twodic Removeobjectsforkeys:keyarray];      NSLog (@ "%@", twodic);        Create Threedic by Twodic Value nsmutabledictionary* threedic = [Nsmutabledictionary dictionary];    nsarray* ValueArray = [Twodic allvalues]; for (int i = 0; i < [ValueArray count]; i++) {nsstring* city = [NSString stringwithformat:@ "%@", [valuearRay Objectatindex:i]];    [Threedic setobject:city Forkey:[valuearray objectatindex:i];    } for (nsstring* key in Threedic) {NSLog (@ "key:%@,value:%@", Key,[threedic Objectforkey:key]); }} return 0;}

OC Language--Arrays & Dictionaries

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.