iOS learning the third day collection class In--objective-c

Source: Internet
Author: User
Tags allkeys set set
<span id="Label3"></p><p><p>The collection class in Objective-c:</p></p><p><p>1. Array Nsarray</p></p><p><p>In objective-c, the collection class consists of immutable array--nsarray, variable array--nsmutablearray,</p></p><p><p>Immutable dictionary--nsdictionary, Variable dictionary--nsmutablearray,</p></p><p><p>Immutable set--nsset, mutable Set--nsmutableset</p></p><p><p>1>. Immutable Array Nsarray</p></p><p><p>An array is a collection of ordered objects used to store an Object's <span style="text-decoration: underline;"><strong>serialized table</strong></span> , an object that must exist in an array in oc, not a basic data type, and if you want to deposit the basic data type, you must first convert the data type to an object and then into the collection class. Nsarray is also an object in OC and needs to be instantiated using nsarray.</p></p><p><p>(1). Initialization of Nsarray:</p></p> <ul> <li><li>Nsarray convenience initialization function:-(id) initwithobjects: (id) firstobject, ....;</li></li> <li><li>Nsarray convenient constructor: + (id) arraywithobjects: (id) firstobject, ...;</li></li> <li><pre><span style="color: #008000;"><span style="color: #008000;"> //</span></span><span style="color: #008000;"><span style="color: #008000;">convenient initialization of the Nsarray</span></span>Nsarray *array1 = [[nsarray alloc] initwithobjects:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">AAA</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">BBB</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">CCC</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, nil]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Nsarray's Handy Builder</span></span>Nsarray *array2 = [nsarray arraywithobjects:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">111</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">222</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">333</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, nil];</pre><p><p>(2). gets the number of array elements and the elements of the group</p></p><p><p>-(nsuinteger) count; Gets the number of array elements</p></p><p><p>-(id) objectatindex: (nsuinteger) index;</p></p></li> </ul><pre><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Count = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) [array1 count]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>element = [array1 Objectatindex:<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">array1_count =%d, array[0] =%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, count, element);<br>   <strong>//traverse The elements of the Array1</strong> </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i =<span style="color: #800080;"><span style="color: #800080;">0</span></span>; I < [array1 count]; i++<span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>temp =<span style="color: #000000;"><span style="color: #000000;">[array1 objectatindex:i]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, temp); }</span></span></pre><p><p>2>. Variable array Nsmutablearray</p></p><p><p>The capacity of the Nsarray is fixed, and the capacity of Nsmutablearray is variable, we can initialize a capacity when the Nsmutablearray is instantiated, but this capacity is not fixed and will automatically increase when not enough. Nsmutablearray is a subclass of nsarray, an extension of nsarray.</p></p><p><p>(1) Initialization of Nsmutablearray</p></p><p><p>Nsmutablearray Convenient initialization function:-(id) initwithcapacity: (nsuinteger) numitems;</p></p><p><p>Nsmutablearray convenient constructor: + (id) arraywithcapacity: (nsuinteger) numitems;</p></p><p><p>(2) Adding and deleting elements</p></p><p><p>Add Element:-(void) addobject: (id) anobject; --add elements to the tail of the array</p></p><p><p>Delete all Contents:-(void) removeallobjects;</p></p><p><p>Delete the last Element:-(void) removelastobject;</p></p><p><p>Delete element by Index:-(void) removeobjectatindex: (nsuinteger) index;</p></p><p><p>Delete any element:-(void) removeobject: (id) object;</p></p><p><p>(3) insertion and substitution of elements</p></p><p><p>Replace the element with the index of the Array:-(void) replaceobjectatindex: (nsuinteger) index withobject: (id) anobject;</p></p><p><p>Inserts an object at the specified index:-(void) insertobject: (id) anobject atindex: (nsuinteger) index;</p></p><pre>Array1 = [nsmutablearray arraywithobjects:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">111</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">222</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">333</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, nil]; [array1 replaceobjectatindex:</span></span><span style="color: #800080;"><span style="color: #800080;">0</span></span>Withobject:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">AAA</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">,</span></span><span style="color: #000000;"><span style="color: #000000;">array1);</span></span><span style="color: #000000;"><span style="color: #000000;">[array1 insertobject:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Ranjin</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Atindex:<span style="color: #800080;"><span style="color: #800080;">1</span></span>];</pre><p><p>(4) Array Traversal</p></p><p><p>(use enumerators and quick enumerations Here)</p></p><p><p>1>. Enumerator (iterator): to iterate through an array through nsenumerator, first get the enumerator through-(nsenumerator *) objectenumerator, and get the object by enumerating the Nextobject in it.</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">Get Enumerator</span> Nsenumerator *enumerator =<span style="color: #000000;"> [array1 objectenumerator]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Temp Variable</span> <span style="color: #0000ff;">ID</span> <span style="color: #000000;">obj; </span> <span style="color: #0000ff;"></span> while (obj =<span style="color: #000000;"> [enumerator nextobject]) { NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, obj); }</span></pre></pre><p><p>2>. Fast-track calendars, equivalent to the use of foreach in PHP</p></p><pre><pre> <span style="color: #0000ff;"></span> for (<span style="color: #0000ff;">ID</span><span style="color: #0000ff;"></span> in<span style="color: #000000;"> array1) { NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, obj); }</span></pre></pre><p><p>2. Dictionary (Dictionary)</p></p><p><p>A dictionary is like a map in java, where a key value pair is stored, and the<span style="text-decoration: underline;"><strong>value of key cannot be duplicated</strong></span> . Dictionaries can be changed and Non-immutable.</p></p><p><p>1. Immutable Dictionary Nsdictionary</p></p><p><p>(1) dictionary creation and acquisition of dictionary values</p></p><pre>Nsdictionary *dictionary = [nsdictionary<strong><strong>Dictionarywithobjectsandkeys</strong></strong>:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">value1</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Key1</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">value2</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Key2</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, nil];//initialization of immutable dictionaries (note order)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>Value = [dictionary<strong><strong>Objectforkey</strong></strong>:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Key1</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">];//gets The value of the dictionary NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;">The value of the <span style="color: #800000;">key1 is%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, value);</pre><p><p>(2) the traversal of the dictionary</p></p><p><p>The way the dictionary is traversed is through the Dictionary-(nsarray *) AllKeys gets all the keys in the array and then gets the value corresponding to the key by iterating through the array, encapsulating the function as Follows:</p></p><pre><pre><span style="color: #000000;"> </span>*keys =<span style="color: #000000;"><strong>allKeys</strong>]; </span> <span style="color: #0000ff;"></span> for (<span style="color: #0000ff;">ID</span><span style="color: #0000ff;"></span> in<span style="color: #000000;"> keys) { NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@==>%@</span><span style="color: #800000;">"</span> <span style="color: #000000;"> <strong> Objectforkey</strong>: obj]); }</span></pre></pre><p><p>2. Variable dictionary nsmutabledictionary</p></p><p><p>When using the variable dictionary nsmutabledictionary, you can initialize a space for a mutable dictionary, and if not enough, it will automatically increase</p></p><p><p>Assigning initialization space to a mutable dictionary: + (id) dictionarywithcapacity: (nsuinteger) num;</p></p><p><p>Add Object:-(void to the mutable dictionary) setobject (id) obj forkey (id) akey;</p></p><p><p>Delete object based on Keyword:-(void) removeobjectforkey: (id) akey;</p></p><p><p>Delete all data in the Dictionary:-(void) removeallobjects;</p></p><pre>Nsmutabledictionary *dictionary = [nsmutabledictionary dictionarywithcapacity:<span style="color: #800080;"><span style="color: #800080;">3</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">adding elements</span></span>[dictionary setobject:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">CEO</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Forkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Nick</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [dictionary setobject:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Manager</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Forkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Isabel</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [dictionary setobject:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">CTO</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Forkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Nicole</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, dictionary); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">remove Element</span></span>[dictionary removeobjectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Nick</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, dictionary);</pre><p><p>3. Set Set</p></p><p><p>? the <span style="text-decoration: underline;"><strong>set</strong></span> Set class <span style="text-decoration: underline;"><strong>is unordered and non-repetitive</strong></span> like the set in our math, and the set can only hold objects, as well as mutable set Nsmutableset and immutable set Nsset.</p></p><p><p>There is a problem, recorded, I declare the Array_display () method in the interface file, implement this method in the implementation file, but in the main.m file, after the new array array object, why the object cannot call that method?? But only in the main method to Achieve.</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;"> array_display (settoarray);</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Why doesn't it work</span> ? Nsenumerator *enumerator =<span style="color: #000000;"> [settoarray objectenumerator]; </span> <span style="color: #0000ff;">ID</span> <span style="color: #000000;">obj; </span> <span style="color: #0000ff;"></span> while (obj =<span style="color: #000000;"> [enumerator nextobject]) { NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, obj); }</span></pre></pre><p><p>? 1. Immutable Set Nsset</p></p><p><p>?    ?    ? ? (1) The initialization of Nsset has its corresponding convenient initialization method and convenience constructor as well as other collection Classes.</p></p><p><p>?    ?    ?    ?    ? ? Convenient initialization method:-(id) initwithobjects: (id) firstobject, ...;</p></p><p><p>?    ?    ?    ?    ? Convenient constructor: + (id) setwithobjects: (id) firstobject, ...;</p></p><p><p>?    ?    ? ? (2) Nsset also has-(nsuinteger) count to get the object of the element</p></p><p><p>?    ?    ? ? (3) gets the elements in the collection</p></p><p><p>?    ?    ?    ?    ? Gets all the collection elements, returned as an array Of:-(nsarray *) allobjects;</p></p><p><p>?    ?    ?    ?    ? :-(id of the element in the get Collection) anyobject;</p></p><p><p>?    ?    ? ? (4) determine whether the two set is the Same:-(BOOL) isequaltoset: (nsset *) otherset;</p></p><p><p>?    ?    ? ? (5) determine whether an element is in this set-(BOOL) member: (id) obj;</p></p><p><p></p></p><p><p>? 2, variable Set:nsmutableset</p></p><p><p>?      ? ? 1. Instantiation and initialization of mutable sets</p></p><p><p>?    ?      ? ? Convenient initialization function:-(id) initwithcapacity: (nsuinteger) numitems;</p></p><p><p>?    ?      ? Convenient constructor: + (id) setwithcapacity: (nsuinteger) numitems;</p></p><p><p></p></p><p><p>?    ?  ? 2. Adding elements to the Mutable collection</p></p><p><p>?    ?      ? ?-(void) addobject: (id) object;</p></p><p><p>?      ? ? 3. Delete objects in the collection</p></p><p><p>?    ?      ? ?-(void) removeallobjects; Delete all the objects;</p></p><p><p>?    ?      ? ?-(void) removeobjects: (id) Object deletes one of the objects;</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">facilitates initialization of function allocation size</span></span>Nsmutableset *set01 = [[nsmutableset alloc]initwithcapacity:<span style="color: #800080;"><span style="color: #800080;">3</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nsmutableset</span></span>*set02 = [nsmutableset setwithcapacity:<span style="color: #800080;"><span style="color: #800080;">3</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">adding elements</span></span>[set01 addobject:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">AA</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [set01 addobject:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">BB</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [set01 addobject:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">cc</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nsarray</span></span>*array =<span style="color: #000000;"><span style="color: #000000;">[set01 allobjects]; Nsenumerator</span></span>*enumerator =<span style="color: #000000;"><span style="color: #000000;">[array objectenumerator]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">obj; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(obj =<span style="color: #000000;"><span style="color: #000000;">[enumerator Nextobject]) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, obj); }</span></span></pre><p><p>Finally: wrapping the basic data type as an object</p></p><p><p>? More than once, the basic data types are not allowed in the set class in oc, so <span style="text-decoration: underline;"><strong>how do we encapsulate the basic data types as objects</strong></span> ? In OC we were given a class specifically designed to encapsulate basic data types as objects, and this class is <span style="text-decoration: underline;"><strong>nsnumber</strong></span>.</p></p><p><p>? 1. The usage of NSNumber is as follows (not yet measured in the Code.) Just put a copy of the other to come, for oneself later Study. )</p></p><p><p>?    ?    ? ? Convenient constructor for wrapping basic types into objects</p></p><p><p>?    ?    ?    ? ?-(id) initwithchar: (char) value;</p></p><p><p>?    ?    ?    ? ?-(id) initwithint: (int) value;</p></p><p><p>?    ?    ?    ? ?-(id) initwithfloat: (float) value;</p></p><p><p>?    ?    ?    ? ?-(id) initwithbool: (BOOL) value;</p></p><p><p>? A convenient constructor that wraps basic data types into objects</p></p><p><p>?    ?    ?    ? ? + (id) Numberwithchar: (char) value;</p></p><p><p>?    ?    ?    ? ? + (id) Numberwithint: (int) value;</p></p><p><p>?    ?    ?    ? ? + (id) Numberwithfloat: (float) value;</p></p><p><p>?    ?    ?    ? ? + (id) Numberwithbool: (BOOL) value;</p></p><p><p>? Get the value from NSNumber</p></p><p><p>?    ?    ?    ?    ?-(char) charvalue;    ?      ?-(int) intvalue;    ?-(float) floatvalue; ?    (BOOL) boolvalue; ? (nsstring *) stringvalue;</p></p><p><p>2, in the collection class is not to store nil (empty), because nil as nil terminator, then how do we store empty objects? The <span style="text-decoration: underline;"><strong>NSNull</strong></span> , whose function is to wrap the null into an object,</p></p><p><p>?    ?    ?    ? ? + (NSNull *) null;</p></p><p><p>iOS learning the third day collection class In--objective-c</p></p></span>

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.