IOS-arrays and dictionaries (nsarray & Nsdictionary)
Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Common ways to handle arrays</p></p><p><p>--------------------non-variable groups</p></p><p><p>1. Creation of arrays</p></p><p><p>NSString *s1 = @ "zhangsan";</p></p><p><p>NSString *s2 = @ "lisi";</p></p><p><p>NSString *S3 = @ "wangwu";</p></p><p><p>(1)</p></p><p><p>Nsarray *array1 = [[nsarray alloc] initwithobjects:s1,s2,s3, nil];</p></p><p><p>NSLog (@ "%@", array1); Equivalent to Array1.descripton</p></p><p><p></p></p><p><p>(2) use the class method to create</p></p><p><p></p></p><p><p>Nsarray *array2 = [nsarray arraywithobjects:s1,s2,s3, nil];</p></p><p><p>(3) Create an Array object while depositing an element into it</p></p><p><p>Nsarray *array3 = [nsarray arraywithobject:s1];</p></p><p><p></p></p><p><p>(4) create an array in which the elements in this array come from array1</p></p><p><p>Nsarray *array4 = [nsarray arraywitharray:array1];</p></p><p><p>NSLog (@ "array4 =%@", array4);</p></p><p><p></p></p><p><p>2. Take the element by subscript</p></p><p><p>NSString *str1 = [array4 objectatindex:0];</p></p><p><p></p></p><p><p>3. Number of array elements</p></p><p><p></p></p><p><p>Nsuinteger count = [array4 count]; Equivalent To: array4.count;</p></p><p><p></p></p><p><p>4. Determine if an element is contained in an array</p></p><p><p>BOOL iscontains = [array4 containsobject:@ "zhangsan"];</p></p><p><p>NSLog (@ "iscontains:%d", iscontains);</p></p><p><p></p></p><p><p>5. Find the subscript position of an object in an array</p></p><p><p>Nsuinteger index = [array4 indexofobject:@ "wangwu"];</p></p><p><p>if (index = = Nsnotfound) {</p></p><p><p>NSLog (@ "not Find elemnts");</p></p><p><p>} else {</p></p><p><p>NSLog (@ "index =%ld", index);</p></p><p><p>}</p></p><p><p></p></p><p><p>6. Link strings in an array (provided that the array is all Strings)</p></p><p><p>NSString *joinstring = [array4 componentsjoinedbystring:@ ","];</p></p><p><p>NSLog (@ "joinstring =%@", joinstring);</p></p><p><p></p></p><p><p>7. Accessing the last element of an array</p></p><p><p>NSString *lastobj = [array4 lastobject]; Array4.lastobject</p></p><p><p>NSLog (@ "lsatobj =%@", lastobj);</p></p><p><p></p></p><p><p>8. Add an element behind the array of the original path</p></p><p><p></p></p><p><p>Nsarray *array5 = [array4 arraybyaddingobject:@ "zhaolia"];</p></p><p><p>NSLog (@ "array5 =%@", array5);</p></p><p><p></p></p><p><p>Take the corresponding subscript element</p></p><p><p>int idx=4;</p></p><p><p>If (idx <array5.count) {</p></p><p><p>NSString *s = [array5 objectatindex:idx];</p></p><p><p>NSLog (@ "s =%@", s);</p></p><p><p>}</p></p><p><p></p></p><p><p>--------------the traversal of an array</p></p><p><p>1. Normal traversal</p></p><p><p>For (int i=0; i<array5.count; I++) {</p></p><p><p>NSString *str = [array5 objectatindex:i];</p></p><p><p>NSLog (@ "%@", str);</p></p><p><p>}</p></p><p><p></p></p><p><p>Fast traversal</p></p><p><p>For (nsstring *s in Array5) {</p></p><p><p>NSLog (@ "%@", s);</p></p><p><p>}</p></p><p><p></p></p><p><p>Optimization after 4.4</p></p><p><p>1. Create a vegetarian group</p></p><p><p>Nsarray *array7 = @[s1,s2,s3]; Equivalent to: Nsarray *array7 = [nsarray arraywithobjects:s1,s2,s3,nil];</p></p><p><p>NSLog (@ "array7 =%@", array7);</p></p><p><p></p></p><p><p>NSString *str = array7[0];</p></p><p><p>NSLog (@ "array[7] =%@", str);</p></p><p><p></p></p><p><p></p></p><p><p>------------------variable Array</p></p><p><p>initialize, set the number of elements is 5, can be changed. (inherited from Nsarray)</p></p><p><p>Nsmutablearray *mutablearray = [nsmutablearray arraywithcapacity:5];</p></p><p><p></p></p><p><p>Want to add an element to the array</p></p><p><p>[mutablearray addobject:@ "aaa"];</p></p><p><p>Inserts an element into an array by specifying the subscript</p></p><p><p>[mutablearray insertobject:@ "ccc" atindex:0];</p></p><p><p>NSLog (@ "%@", mutablearray); The original position element is moved back</p></p><p><p></p></p><p><p>Remove the last element</p></p><p><p>[mutablearray removelastobject];</p></p><p><p>NSLog (@ "after Removing the last element:%@", mutablearray);</p></p><p><p></p></p><p><p>Removing the specified element</p></p><p><p>[mutablearray removeobject:@ "aaa"];</p></p><p><p></p></p><p><p>Remove the specified subscript element</p></p><p><p>[mutablearray removeobjectatindex:0];</p></p><p><p></p></p><p><p>To add an array to an array</p></p><p><p>[mutablearray addobjectsfromarray:array1];</p></p><p><p></p></p><p><p>1. Creating a mutable array</p></p><p><p>NSString *t1 = @ "zhangsan";</p></p><p><p>NSString *t2 = @ "lisi";</p></p><p><p>NSString *T3 = @ "wangwu";</p></p><p><p></p></p><p><p>Nsmutablearray *marray1 = @[s1,s2,s3];//wrong. An immutable group was created here</p></p><p><p>Nsmutablearray *marray1 = [[nsmutablearray alloc] initwithobjects:s1,s2,s3, nil];</p></p><p><p></p></p><p><p>When creating an array, 3 spaces are created to store elements, storage exceeds capacity, and arrays automatically increase space</p></p><p><p>Nsmutablearray *marray2 = [[nsmutablearray alloc] initwithcapacity:3];</p></p><p><p></p></p><p><p>Nsmutablearray *marray3 = [nsmutablearray arraywithcapacity:3];</p></p><p><p></p></p><p><p>2. Adding elements</p></p><p><p>[mArray2 addobject:t1];</p></p><p><p>[mArray2 addobject:t2];</p></p><p><p>[mArray2 addobject:t3];</p></p><p><p>NSLog (@ "marray2=%@", mArray2);</p></p><p><p></p></p><p><p>Adding elements from MArray2 to MArray3</p></p><p><p>[mArray3 addobjectsfromarray:marray2];</p></p><p><p></p></p><p><p>Add MArray2 as a two-dimensional number</p></p><p><p>[mArray3 addobject:marray2];</p></p><p><p>NSLog (@ "mArray3 =%@", mArray3);</p></p><p><p></p></p><p><p>3. Inserting elements</p></p><p><p>[mArray2 insertobject:@ "Jack" atindex:0];</p></p><p><p>NSLog (@ "mArray2 =%@", mArray2);</p></p><p><p></p></p><p><p>4. Replacing elements</p></p><p><p>[mArray2 replaceobjectatindex:0 withobject:@ "John"];</p></p><p><p>NSLog (@ "replace:%@", mArray2);</p></p><p><p></p></p><p><p>5. Swap the position of two elements</p></p><p><p>[mArray2 Exchangeobjectatindex:3 withobjectatindex:0];</p></p><p><p>NSLog (@ "mArray2 =%@", mArray2);</p></p><p><p></p></p><p><p>6. Deleting an element</p></p><p><p>6.1 Delete According to subscript</p></p><p><p>[mArray2 removeobjectatindex:2];</p></p><p><p>NSLog (@ "mArray2 =%@", mArray2);</p></p><p><p></p></p><p><p>6.2 Delete last Element</p></p><p><p>[mArray2 removelastobject];</p></p><p><p>NSLog (@ "mArray2 =%@", mArray2);</p></p><p><p></p></p><p><p>6.3 Delete the specified object</p></p><p><p>[mArray2 removeobject:@ "zhangsan"];</p></p><p><p></p></p><p><p>6.4 Delete all elements</p></p><p><p>[mArray2 removeallobjects];</p></p><p><p>NSLog (@ "mArray2 =%@", mArray2);</p></p><p><p>****************************************************** ****************************************************** *** *************************************************** ******************************************************</p></p><p><p>2. Common Methods of dictionary processing</p></p><p><p>---------------immutable Dictionaries</p></p><p><p>1. Creation of dictionaries</p></p><p><p>Nsarray *array1 = [nsarray arraywithobjects:@ "zhangsan", @ "zhangfei", nil];</p></p><p><p>Nsarray *array2 = [nsarray arraywithobjects:@ "lisi", @ "liping", nil];</p></p><p><p></p></p><p><p>First Element: key:@ "zhang" value:array1</p></p><p><p>Second Element: key: @ "li:value:array2</p></p><p><p>Nsdictionary *dic1 = [[nsdictionary alloc] initwithobjectsandkeys:array1,@ "zhang", array2,@ "li", nil];</p></p><p><p></p></p><p><p>Nsuinteger count = dic1.count; Number of dictionary elements</p></p><p><p>NSLog (@ "count:%d", count);</p></p><p><p></p></p><p><p>Initializing an element when creating a dictionary</p></p><p><p>Nsdictionary *dic2 = [nsdictionary dictionarywithobject:array1 forkey:@ "zhang"];</p></p><p><p>NSLog (@ "dic2 =%@", dic2);</p></p><p><p></p></p><p><p>2. Get all keys in the dictionary</p></p><p><p>Nsarray *allkeys = [dic1 allKeys]; Dic1.allkeys.</p></p><p><p>NSLog (@ "allkeys=%@", allKeys);</p></p><p><p>3. Get all the value in the dictionary</p></p><p><p>Nsarray *allvalues = [dic1 allvalues];</p></p><p><p>NSLog (@ "allvalues =%@", allvalues);</p></p><p><p></p></p><p><p>4. Get Value by key</p></p><p><p>Nsarray *array3 = [dic1 objectforkey:@ "zhang"];</p></p><p><p>NSLog (@ "array3 =%@", array3);</p></p><p><p></p></p><p><p>How to Optimize------------dictionary</p></p><p><p>Nsdictionary *dic3 = @{@ "zhang": array1,@ "li": array2};</p></p><p><p>NSLog (@ "dic3 =%@", dic3);</p></p><p><p></p></p><p><p>syntax to take value: dictionary ["key"]</p></p><p><p>Nsarray *array4 = dic3[@ "zhang"]; Equivalent to: Nsarray *array3 = [dic3 objectforkey:@ "zhang"];</p></p><p><p>NSLog (@ "array4 =%@", array4);</p></p><p><p></p></p><p><p>--------------variable Dictionaries</p></p><p><p>1. Create a mutable dictionary</p></p><p><p>Nsmutabledictionary *md1 = [[nsmutabledictionary alloc] initwithcapacity:3];</p></p><p><p></p></p><p><p>2. Adding elements</p></p><p><p>[MD1 setobject:array1 forkey:@ "zhang"];</p></p><p><p>[MD1 setobject:array2 forkey:@ "li"];</p></p><p><p>NSLog (@ "md1 =%@", md1);</p></p><p><p></p></p><p><p></p></p><p><p>Add all the elements in the dictionary dic1 to the MD1 dictionary</p></p><p><p>[MD1 addentriesfromdictionary:dic1];</p></p><p><p>NSLog (@ "md1 =%@", md1);</p></p><p><p></p></p><p><p>[MD1 setobject:@ "zhangsan" forkey:@ "zhang"]; If you add the same key, the original value will be overwritten</p></p><p><p></p></p><p><p>3. Delete (delete element according to Key)</p></p><p><p>[MD1 removeobjectforkey:@ "zhang"];</p></p><p><p>NSLog (@ "md1 =%@", md1);</p></p><p><p>Delete all elements (empty)</p></p><p><p>[MD1 removeobjects];</p></p><p><p></p></p><p><p>Delete multiple elements corresponding to multiple keys</p></p><p><p>[MD1 removeobjectsforkeys:@[@ "zhang", @ "li"];</p></p><p><p>NSLog (@ "md1 =%@", md1);</p></p><p><p></p></p><p><p>---------------------traversing A dictionary</p></p><p><p>1. Fast Traversal</p></p><p><p>For (nsstring *key in Md1) {</p></p><p><p>Nsarray *name = [md1 objectforkey:key];</p></p><p><p>NSLog (@ "fast traversal: key =%@,value =%@", key,name);</p></p><p><p>}</p></p><p><p></p></p><p><p>2. Normal traversal</p></p><p><p>Nsarray *allkeys2 = [md1 allKeys];</p></p><p><p>for (int i = 0; i<allkeys.count-1; i++) {</p></p><p><p>NSString *key = allkeys2[i];</p></p><p><p>Nsarray *name = md1[key];</p></p><p><p>NSLog (@ "normal traversal: key =%@,value =%@", key,name);</p></p><p><p>}</p></p><p><p>IOS-arrays and dictionaries (nsarray & Nsdictionary)</p></p></span>
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