OC-collection class
1. the OC collection classes include nsarray, nsset, and nsdictionary. They all operate arrays in an object-oriented manner. Unlike arrays in C, OC Arrays can only store the same data type. They can store any type of objects, however, non-OC object types such as basic data types int, struct, and enum cannot be stored.
2. The OC array exists as an object. Therefore, you must add *
3. Once an nsarray array is created, it determines whether it is variable or not.
4. Basic nsarray array and subclass nsmutablearray operations:
1> use the nsarray class method arry to create an array, which is defined as an empty array. Then, the array will always be an empty array.
Nsarray * array = [nsarray array];
2> there are two ways to create an array object:
Nsarray * array = [nsarray arraywithobjects: @ "Jack", nil];
// The array contains two nsstring objects. Nil is the identifier used to mark the end of an array element.
// Therefore, empty objects cannot be stored in the OC array.
Nsarray * array1 = [nsarrayarraywithobjects: @ "Jack", @ "Rose", nil];
// Method 2:
Nsarray * array2 = @ [@ "Jack", @ "Rose"];
// Obtain the length of the array array1.count;
Nsuinteger COUNT = [array1 count];
Nslog (@ "% lD", count );
// Two methods to access objects in the array
Nsstring * str1 = [array1 objectatindex: 0]; // The object with index 1 in the array. the method provided in OC returns: Jack
Nsstring * str2 = array1 [0]; // The Compiler feature is actually in the above format to access and return: Jack
5. Three Methods for Traversing Arrays
1> common mode:
For (INT I = 0; I <array1.count; I ++)
{
Nslog (@ "% @", array1 [I]); // The object stored in the array can be output using % @
If (I = 0)
{
Break;
}
}
2> assign each object in the array1 array to OBJ and print it in sequence.
For (id obj in array1)
{// Id obj indicates the object in the array
// Obtain the index of the OBJ object in the array
Nsuinteger Index = [array1 indexofobject: OBJ];
Nslog (@ "% LD -- % @", index, OBJ );
}
3> each time a block is used to traverse an element from the array, it is passed to the block. The block is also executed once accordingly.
// The id obj in the block corresponds to the element in the array, and the index bool of the element in the nsuinteger idx corresponds to the element in the array to stop traversal.
[Array1 enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * stop)
{
Nslog (@ "% LD -- % @", idx, OBJ );
// If the index is 0, stop traversing immediately
If (idx = 0)
{
* Stop = yes;
}
};
6. Basic nsmutablearray operations:
1> Create a variable array:
Nsmutablearray * mutablearray = [nsmutablearray array];
Nsmutablearray * mutalbearray2 = @ [@ "Zhang San", @ "Li Si"]; Note: @ [] returns an unchangeable nsarray array, and an error is returned.
// Add any object to the variable array
Person * person = [[person alloc] init];
[Mutablearray addobject: person];
[Mutablearray addobject: @ "Jack"];
[Mutablearray addobject: @ "John"];
// [Mutablearray addobject: 10]; basic data types cannot be stored in arrays.
// Delete the specified element from the array
[Mutablearray removeobject: @ "John"];
[Mutablearray removeobjectatindex: 0];
// Delete all elements in the array
[Mutablearray removeallobjects];
7. basic operations of nsset array and subclass nsmutableset: nsset and nsarray are both immutable arrays, which cannot be changed once created. Nsset is a simple unordered set operation.
1> Create an nsset:
Nsset * Set = [nsset set]; // It is always null once it is created
Nsset * set2 = [nsset setwithobjects: @ "Jack", @ "Rose", @ "Jim", nil];
// Randomly retrieve the elements in the nsset set
Nsstring * STR = [set2 anyobject];
Nslog (@ "% @", STR );
Simple nsmutableset operation:
// Create a variable set
Nsmutableset * mutableset = [nsmutableset set];
// Add a set for Set
[Mutableset addobject: @ "Jack"];
[Mutableset addobject: @ "Rose"];
// Delete an element
[Mutableset removeobject: @ "Jack"];
8. Comparison between nsarray and nsset Arrays:
1> all types of OC objects can be stored, and basic data types cannot be stored.
2> they are all immutable, but their subclasses are all mutable.
3> nsarray is an ordered set and nsset is an unordered set.
9. nsdictionary and nsmutabledictionary:
1> nsdictionary and its subclass nsmutabledictionary both exist in the form of key/value, and nsdictionary itself is an unchangeable set.
2> nsdictionary is also an unordered set.
3> the values stored in the dictionary set exist in the form of key-value pairs. If the same key exists, the values of the subsequent keys will be overwritten. However, vaule duplication is allowed.
10. Basic nsdictionary operations
1> four common ways to create a dictionary set
// 1. Create an empty dictionary set type
Nsdictionary * DIC = [nsdictionary dictionary]; // if it is null, it is permanently empty.
// 2. Create a dictionary set with only one set of values
Nsdictionary * dic1 = [nsdictionary dictionarywithobject: @ "Jack" forkey: @ "name"];
// 3. Create a dictionary set with multiple values
Nsarray * keys = @ [@ "name", @ "Address", @ "E-mail"];
Nsarray * values = @ [@ "Jack", @ "Beijing", @ "[email protected]"];
Nsdictionary * dic3 = [nsdictionary dictionarywithobjects: Values forkeys: Keys];
// 4. Create a value/Key
Nsdictionary * dic4 = [nsdictionary dictionarywithobjectsandkeys:
@ "Jack", @ "name ",
@ "Beijing", @ "Address ",
@ "[Email protected]", @ "E-mail", nil];
// 5. Similar to the array Creation Method
Nsdictionary * dic5 =@ {@ "name": @ "Jack ",
@ "Address": @ "Beijing ",
@ "E-mail": @ "[email protected]"};
// Value: obtain the corresponding value based on the corresponding key.
Nsstring * name = [dic1 objectforkey: @ "name"]; // RETURN jack
// Number of return key-value pairs
Nsuinteger COUNT = dic5.count; // return 4
11. traverse the nsdictionary array in two ways:
1>
Nsarray * allkeys = [dic5 allkeys]; // The obtained keys are unordered in the array.
For (INT I = 0; I <dic5.count; I ++)
{
Nsstring * Key = allkeys [I];
Nsstring * value = dic5 [Key];
Nslog (@ "% @ --> % @", key, value );
}
2> pass the key-value pairs in the dictionary to the keys and OBJ in the block.
[Dic5 enumeratekeysandobjectsusingblock: ^ (idkey, Id OBJ, bool * stop)
{
Nslog (@ "% @ --> % @", key, OBJ );
// Stop immediately after a traversal * Stop = yes;
};
12. Basic nsmutabledictionary operations:
1> Create a variable dictionary
Nsmutabledictionary * mutabledic = [nsmutabledictionary dictionary];
Nsmutabledictionary * mutabledic2 [email protected] {@ "name", @ "Rose", @ "Address", @ "Beijing"}; incorrect syntax because the return on the right side is an immutable dictionary
2> add elements to a variable dictionary
[Mutabledic setobject: @ "Rose" forkey: @ "name"];
[Mutabledic setobject: @ "Beijing" forkey: @ "Address"];
3> remove
[Mutabledic removeallobjects];
[Mutabledic removeobjectforkey: @ "name"];
4> Print
Nslog (@ "% @", mutabledic );