Nsarray is an immutable group in the Foundation framework, let's implement these methods:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) {@autoreleasepool {//oc array can be Storing objects of different types; the array of OC can only store objects;//But memory space is still contiguous; Nsarray *arr1 = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", nil];/
/array finally must end with nil, indicating the end of the array; NSLog (@ "%@", arr1);
Computes the array length; int count = (int) arr1.count;
NSLog (@ "Count =%d", count);
Detects whether there is a value in the array; BOOL iscontainer = [arr1 containsobject:@ "5"];
if (Iscontainer) {NSLog (@ "exists this object");
}else{NSLog (@ "There is no such object");
//Get the last object; NSString *lastobj = [arr1 lastobject];
NSLog (@ "lastobj =%@", lastobj);
Get the first object; NSString *firstobj = [arr1 firstobject];
NSLog (@ "firstobj =%@", firstobj);
Gets the object at the specified position; nsstring *indexobj = [arr1 objectatindex:2];
NSLog (@ "indexobj =%@", indexobj);
Gets the location of a specified object;//If the element does not exist, print-1; int index = (int) [arr1 indexofobject:@ "2"];
NSLog (@ "index =%d", index); } RETurn 0; }
The output results are as follows:
.
GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.