OC: Collection class in OC-NSArray (1)
/* Set classes in OC-
NSArray is used for object Ordered Sets.
NSSet is used for unordered objects.
NSDictionary for key-value ing
These three collection classes are immutable (once initialized, they cannot be changed)
(Variable collection class. These three variable collection classes correspond to the subclasses of the above three collection classes)
NSMutableArray NSMutableSet NSMutableDictionary
These collection classes can only be cell phone Cocoa objects (NSObject objects), if you want to save some original C data (such as int, float, double, BOOL, etc ), you need to encapsulate the original C data into the NSNumber type. The NSNumber object is a Cocoa object and can be saved in the Collection class.
*/
/*
NSArray OC Array
*/
// ================================================ ======================================
// C array: All elements must be of the same data type
Int age [] = {1, 2, 3 };
Age [1]; // retrieve the elements in the array
// Put the car in the C Array
Car * car1 = [[Car alloc] init];
Car * cAry [3] = {car1, [Car new], [Car new]};
// ================================================ ======================================
// Create an OC array. ary1 is an empty array (this method is not required)
NSArray * ary1 = [NSArray array];
// Ary2 has an element during initialization.
NSArray * ary2 = [NSArray arrayWithObject: @ "jereh"];
// Create an array with Multiple Elements
// Nil cannot be the default value. It is the end mark of the array and cannot be saved as an element in the array.
NSArray * ary3 = [NSArray arrayWithObjects: @ "h", @ "e", @ "kong", nil];
// Create an array quickly. @ [] nil cannot be saved.
NSArray * ary4 = @ [@ "c", @ "s", @ "d", @ "n"];
// ================================================ ======================================
// The number of OC array elements-(unsigned) Count;
NSArray * ary5 = @ [@ "c", @ "s", @ "d", @ "n"];
[Ary5 count];
// Ary5.count;
NSLog (@ "Number of array elements: % ld", ary5.count );
// ================================================ ======================================
// Access the array element-(id) objectAtIndex :( unsigned int) index; // obtain the object at the specified index
NSArray * ary6 = @ [@ "c", @ "s", @ "d", @ "n"];
// [Ary6 objectAtIndex: 0]; // obtain the element at a position in the array
NSLog (@ "Access array: % @", [ary6 objectAtIndex: 0]);
NSLog (@ "Access array: % @", ary6 [0]); // a simple method for C Arrays
// ================================================ ======================================
// 1. traverse the OC array (output in order)
Car * car = [[Car alloc] init];
NSArray * ary7 = @ [@ "a", @ "B", @ "c", @ "d", @ "e", @ "f ", @ "g", @ "h", car];
For (int I = 0; I
NSLog (@ "traverse array ary7: % @", ary7 [I]);
}
/* 2. Quickly traverse the OC array (fast enumeration): all elements in the array are OC objects, so they are all id types;
Indicates that every element in ary7 is assigned to obj and then cyclically
Disadvantage: you do not know the position of the retrieved object in the array.
Restriction: do not add or delete pointers in the array during enumeration.
*/
Int I = 1;
For (id obj in ary7 ){
NSLog (@ "Fast traversal of % I: % @", I, obj );
I ++;
}
/* 3. Use block to traverse the Array
The block has no return value. It has three parameters: the current object of obj, The idx position, and * stop indicates stopping traversal.
Each time an object is traversed, the block is called once.
(The block part of the method parameter is automatically generated by pressing the Enter key. You do not need to manually write the block part)
*/
[Ary7 enumerateObjectsUsingBlock:
^ (Id obj, NSUInteger idx, BOOL * stop ){
// BOOL * stop
If (idx> 0 ){
* Stop = YES;
}
NSLog (@ "block fast traversal of the % ld element: % @", idx, obj );
}
];
// ================================================ ======================================
/* Other usage
-(BOOL) containsObject: obj: determines whether the array contains the object obj
-(Id) objectAtIndex: the object where I is stored
-(NSUInteger) indexOfObject: The first index number of obj containing the obj element pair
/*
NSMutableArray OC variable array
Is a subclass of NSArray, similar to NSArray, but can add or delete pointers.
*/
// ================================================ ==============================
// Create an empty array
NSMutableArray * ary1 = [NSMutableArray array];
// Add Element
[Ary1 addObject: @ "a"];
[Ary1 addObject: @ "d"];
NSLog (@ "ary1% @", ary1 );
// Add Multiple Elements
NSMutableArray * ary2 = [NSMutableArray array];
[Ary2 addObjectsFromArray: ary1];
NSLog (@ "ary2% @", ary2 );
// ================================================ ==============================
// Delete an element
NSMutableArray * ary3 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", nil];
// Delete an element based on the index location
[Ary3 removeObjectAtIndex: 0];
NSLog (@ "ary3% @", ary3 );
// Delete a specified Element
[Ary3 removeObject: @ "B"];
NSLog (@ "ary3% @", ary3 );
// Delete all elements
[Ary3 removeAllObjects];
// Delete the last element
[Ary3 removeLastObject];
NSMutableArray * ary4 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", nil];
// Find a specific element within the specified range to delete it
[Ary4 removeObject: @ "B" inRange: NSMakeRange (0, 2)];
NSLog (@ "ary4% @", ary4 );
// Delete an element in a certain range
NSMutableArray * ary5 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", nil];
[Ary5 removeObjectsInRange: NSMakeRange (0, 3)];
NSLog (@ "ary5% @", ary5 );
// Deleted the element that already exists in the following Array
NSMutableArray * ary6 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", nil];
NSArray * dAry = @ [@ "d"];
[Ary6 removeObjectsInArray: dAry];
NSLog (@ "ary6% @", ary6 );
// ================================================ ==============================
// NSArray
NSArray * ary7 = @ [@ "aa", @ "bb"];
// Add all elements in another array to the current array (reset the array element and the original element is deleted)
NSMutableArray * ary8 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", @ "e", @ "f ", nil];
[Ary8 setArray: ary7];
NSLog (@ "ary8% @", ary8 );
// Add multiple elements (add new array elements at the end of the original array)
NSMutableArray * ary9 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", @ "e", @ "f ", nil];
[Ary9 addObjectsFromArray: ary7];
NSLog (@ "ary9% @", ary9 );
// Add an element to the index position
[Ary9 insertObject: @ "jereh" atIndex: 1];
NSLog (@ "ary9% @", ary9 );
// ================================================ ==============================
// Replace
NSMutableArray * ary10 = [NSMutableArray arrayWithObjects: @ "a", @ "B", @ "c", @ "d", @ "e", @ "f ", nil];
[Ary10 replaceObjectAtIndex: 2 withObject: @ "apple"];
NSLog (@ "ary10% @", ary10 );
// Replace the element of a range in the current array with a new array
[Ary10 replaceObjectsInRange: NSMakeRange (0, 3) withObjectsFromArray: @ [@ "Hello"];
NSLog (@ "ary10% @", ary10 );
// Swap the elements at one or two index locations in the current array
[Ary10 exchangeObjectAtIndex: 0 withObjectAtIndex: 2];
NSLog (@ "ary10% @", ary10 );
// ================================================ ==============================
// URL resolution
NSString * str = @ "http://www.baidu.com:/agsdha ";
NSArray * ary11 = [str componentsSeparatedByString: @ "//"];
// NSString * str1 = ary11 [1];
NSArray * ary12 = [ary11 [1] componentsSeparatedByString: @ ":"];
NSArray * ary13 = [[ary11 lastObject] componentsSeparatedByString: @ ":"]; // use lastObject to get the last element
NSLog (@ "ary11% @", ary11 );
NSLog (@ "ary12% @", ary12 );
NSLog (@ "ary13% @", ary13 );
NSLog (@ "% @", ary12 [0]);
// Allocate capacity to the array
NSMutableArray * array = [NSMutableArray arrayWithCapacity: 20];