OBJ-C only adds a "special corpus" C language, so you can use printf () instead of nslog (). However, we recommend that you use nslog because it adds features such as timestamp, date stamp, and automatic line break ('\ n.
1. The OC array members are arbitrary object pointers. Similar to the linked list structure in C (ending with nil), all Array Operations cannot cross-border.
The array of OC is divided into immutable array nsarray variable array nsmutablearray
- Nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", nil];
- Nslog (@ "% @", [array objectatindex: 0]); // obtain array members by subscript (subscript starts from 0)
- Nslog (@ "% lD", [array count]); // obtain the number of valid members of the array (excluding nil)
- For (I = 0; I <[array count]; I ++) {// traverses the Array (C method)
- Nslog (@ "% @", [array objectatindex: I]);
- }
- Nslog (@ "% @", array); // (OC) view the array content (send the description message to array first, and then send a description message to each member)
- Nsarray * array1 = [[nsarray alloc] initwitharray: array]; // creating an array object is equivalent to copying
- Nsarray * arry2 = [nsarray arraywitharray: array]; // creating an array object is equivalent to copying
- Nsenumerator * enumer = [array objectenumerator]; // Positive Sequence enumerator
- // Create an enumerator to assign the address of each element of the array to the enumerator once, and then establish an association (the enumerator can only be used to read group members)
- // When enumeration is performed, it creates an association with the array (modifying the monitor/iterator) to restrict enumeration, And the element cannot be modified. It can only be read or written.
- Id OBJ;
- // In the first loop, the first element of the array is assigned to OBJ. In the second loop, the second array element is assigned to OBJ.
- While (OBJ = [enumer nextobject]) {// traverses the Array
- Nslog (@ "% @", OBJ );
- }
- // Fast Enumeration The first loop assigns the first element of array to OB the second loop and assigns the second element of array to OB until nil (read only and cannot be changed)
- For (ID ob in array ){
- Nslog (@ "% @", ob );
- }
- // Output an array in reverse order (create an enumerator in reverse order and assign the last element to OBJ In the first loop)
- Nsenumerator * enumer1 = [array reverseobjectenumerator];
- While (OBJ = [enumer1 nextobject]) {
- Nslog (@ "% @", OBJ );
- }
- If ([OBJ iskindofclass: [Dog class]) // obtain the class type to determine whether it is the object of the specified class
- If ([OBJ ismemberofclass: [Dog class]) {
- }
2. Immutable Array
- ------- Search
- Nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "one", nil];
- Nsuinteger Index = [array indexofobject: @ "one123"]; // returns nsnotfound if the subscript corresponding to the first found array member is not found
- Index = [array indexofobject: @ "one" inrange: nsmakerange (1, 3)]; // search within the specified range
- If (index! = Nsnotfound ){
- Nslog (@ "% lD", index );
- }
- ---- Extract to form a new array
- Nsarray * array1 = [array objectsatindexes: [nsindexset indexsetwithindexesinrange: nsmakerange (1, 3)];
- NUMBER SET
- Nsindexset this is a digital collection class
- [Nsindexset indexsetwithindexesinrange: nsmakerange (1, 3)] generates a set of numbers
3. Variable
- Nsmutablearray * array = [[nsmutablearray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four", nil];
- [Array addobject: @ "five"]; // insert an element at the end of the array
- [Array insertobject: @ "six" atindex: 5]; // insert an element in the specified subscript position of the array (the maximum value is length)
- [Array removeobject: @ "six"]; // delete a specified Element (all positions in the array will be deleted)
- [Array removeobject: @ "two" inrange: nsmakerange (0, 3)]; // delete a specified element within the specified length starting from the specified subscript position
- [Array removelastobject]; // Delete the last element
- [Array removeallobjects]; // deletes all elements.
- [Array replaceobjectatindex: 3 withobject: @ "iOS"]; // Replace the specified subscript position element with the specified element.
- [Array exchangeobjectatindex: 0 withobjectatindex: 3]; // exchange two elements of the specified subobject
4. string segmentation and concatenation
- // @ "" Empty string object
- ------- Segmentation
- Nsstring * PTR = @ "I Am a man ";
- Nsarray * array = [PTR componentsseparatedbystring: @ ""]; // returns the nsarray immutable array as the delimiter.
- Nsmutablearray * array1 = [nsmutablearray arraywitharray: array]; // if modified, nsarray is converted to nsmutablearray.
- Nsarray * array2 = [PTR componentsseparatedbycharactersinset: [nscharacterset charactersetwithcharactersinstring: @ ","]; // use the characters in the string as the condition for segmentation
- Character Set combination
- Nscharacterset this is a character set class.
- [Nscharacterset charactersetwithcharactersinstring: @ ","] // converts a string to a character set combination
- ------- Splicing
- Nsstring * STR = [array componentsjoinedbystring: @ ""];
- If the split condition appears at the beginning or end, an empty string @ "will appear. If not, convert it to nsmutablestring to process the empty string.
- Func1: [array1 removeobject: @ ""]; // directly delete an empty string
- Func2: For (id obj in array1 ){
- If ([OBJ length] = 0) // The length of the empty string is 0.
- If ([OBJ isequaltostring: @ ""]) // compare it with an empty string (the string cannot be =). Use a function)
- }