Document directory
- Nsarray
- Variable Array
- Enumeration
- Quick Enumeration
- Nsdictionary
Cocoa provides many collection classes for use. The following describes how to use the objects.
Nsarray
Nsarray is a cocoa class used to store an ordered list of objects. You can put any type of objects in it: nsstring or other objects you want to store, but it can only be an object that stores objective-C, rather than basic data types in C, such as int, float, Enum, struct, or random pointers in nsarray, at the same time, nil (zero or null value of the object) cannot be stored in nsarray ). As long as an nsarray exists, you can operate on it in various ways. For example, you can point the instance object of an object to this array and pass the array as a parameter, obtain the number of objects stored in the data, obtain the corresponding Objects Based on the index, find the objects in the array, and traverse the array.
Arraywithobjects: Creates a new nsarray and sends a list of objects separated by commas (,). Adding nil at the end of the list indicates that the list has ended. This is one of the reasons why the array cannot store nil.
Nsarray * array;
Array = [nsarray arraywithobjects:
@ "One", @ "two", @ "three", nil];
Use nsarray's count: method to get the number of arrays;
-(Unsigned) count;
You can also obtain the objects at a specific index as follows:
(ID) objectatindex: (unsigned INT) index;
Example:
Int I;
For (INT I = 0; I <[array count]; I ++ ){
Nslog (@ "Index % d has %", I, [array objectatindex: I]);
}
Output result: omitted.
If the referenced index count is greater than the number of objects in the array, an error is returned during running.
Variable Array
Like nsstring, nsarray creates an immutable object. Once you create an array with a specified number of objects, it is fixed: No element can be added or deleted. Of course, the objects in the array can be changed, and the array itself will not change.
Therefore, a new variable array is generated.NsmutablearrayIn this way, you can add or delete objects in the array at will.
The common methods are as follows:
Arraywithcapacity: Create a new variable array:
+ (ID) arraywithcapacity: (unsigned) numitems;
Example:
Nsmutablearray * array;
Array = [nsmutablearray arraywithcapacity: 17];
Addobject: add an object to the end of the array
-(Void) addobject :( ID) anobject;
Removeobjectatindex: delete objects at a specific index.
-(Void) removeobjectatindex: (unsigned) index;
Enumeration
If you want to write a loop from 0 to [array count] to read the objects at each index, you can also use nsenumerator, which is a cocoa method used to describe this set iteration operation. To use nsenumerator, you must useObjectenumeratorRequest the enumerator to the array:
-[Nsenumerator *] objectenumerator;
You can use this method as follows:
[Nsenumerator * enumerator;
Enumerator = [array objectenumerator];
You can also useReverseobjectenumerator.
After obtaining the enumerator, you can start a while loop. Each loop requests its nextobject (next object) from this enumerator ):
-(ID) nextobject;
When nextobject returns the nil value, the loop ends, which is another reason that nil values cannot be stored in the array: we cannot determine whether NIL is a value stored in the array or a flag indicating the end.
The entire cycle is as follows:
Nsenumerator * enumerator;
Enumerator = [array objectenumerator];
Id thingie;
While (thingie = [enumerator nextobject])
{
Nslog (@ "I found % @", thingie );
}
Note and doubt: if you want to use the enumerator, you cannot add or delete such methods to operate the variable array. Otherwise, the problem may occur. This is a problem. Try it!
Quick Enumeration
For (nsstring * string in array ){
Nslog (@ "I found % @", string );
}
In this way, there are three methods to traverse the array: Index, nsenumerator, and quick enumeration.
Nsdictionary
Nsdictionary stores a value under a given keyword (can be any type of object ). Then you can use this keyword to find the corresponding value. Nsdictionary is an unchangeable object like nsarray and nsstring, but the nsmutabledictionary class allows you to add or delete dictionary elements at will. When creating a new nsdictionary, you must provide all the objects and Keywords of the dictionary.
Dictionarywithobjectsandkeys: Create a dictionary.
+ (ID) dictionarywithobjectsandkeys :( ID) firstobject, (nsstring) firstkey ,,,,;
A system where dictionary accepts alternate storage of objects and keywords, with the nil value as the Terminator.
Example:
Tire * T1 = [Tire new];
Tire * t2 = [Tire new];
Tire * T3 = [Tire new];
Tire * t4 = [Tire new];
Nsdictionary * tires;
Tires = [nsdictionary dictionarywithobjectsandkeys: T1, @ "front-left", T2, @ "front-Right", T3, @ "back-left", T4, @ "back-right", nil];
Objectforkey: used to obtain the value in the dictionary and pass the keyword used to store the value to the method.
-(ID) objectforkey: (ID) akey;
Therefore, to find an object in the dictionary, you can write as follows:
Tire * tire = [Tires objectforkey: @ "back-left"];
If the object cannot be found based on the keyword, objectforkey: returns the nil value.
To create a new numutabledictionary object, send a dictionary message to the nsmutabledictionary class. You can also use the dictionarywithcapacity: method to create a new variable dictionary and tell the final size.
+ (ID) dictionarywithcapacity: (unsigned INT) numitems;
Setobject: forkey: The method adds an element to the dictionary:
-(Void) setobject: (ID) anobject forkey: (ID) akey;
The following is another method for creating a dictionary for storing tires:
Nsmutabledictionary * tires;
Tires = [nsmutabledictionary dictionay];
[TIRES setobject: T1 forkey: @ "front-left"];
Note: If you use setobject: forkey: for existing keywords in the dictionary, this method replaces the original value with a new value. If you want to delete a keyword in a variable dictionary, you can use removeobjectforkey: method:
-(Void) removeobjectforkey :( ID) akey;
Example: [Tires removeobjectforkey: @ "back-left"];