Arrays can be said to be the basic operations that software developers face every day. Next we will share some basic nsarray operation code examples for Apple developers to refer, the first line of each code section describes the purpose of the code section by commenting.
/*************************************** **************************************** ************
Nsarray
**************************************** **************************************** ***********/
/* --------------------------- Create an array ------------------------------*/
Nsarray * array = [nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four", nil];
Self. dataarray = array;
[Array release];
//-(Unsigned) count; number of objects contained in the array;
Nslog (@ "self. dataarray cound: % d", [self. dataarray count]);
//-(ID) objectatindex: (unsigned INT) index; get the object at the specified index;
Nslog (@ "self. dataarray cound 2: % @", [self. dataarray objectatindex: 2]);
/* ------------------------ Copy data from an array to another array (variable level )----------------------------*/
// Arraywitharray:
Nsarray * array1 = [nsarray alloc] init];
Nsmutablearray * mutablearray = [nsmutablearray alloc] init];
Nsarray * array = [nsarray arraywithobjects: @ "A", @ "B", @ "C", nil];
Nslog (@ "array: % @", array );
Mutablearray = [nsmutablearray arraywitharray: array];
Nslog (@ "mutablearray: % @", mutablearray );
Array1 = [nsarray arraywitharray: array];
Nslog (@ "array1: % @", array1 );
// Copy
Id OBJ;
Nsmutablearray * newarray = [nsmutablearray alloc] init];
Nsarray * oldarray = [nsarray arraywithobjects:
@ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "g", @ "H ", nil];
Nslog (@ "oldarray: % @", oldarray );
For (INT I = 0; I <[oldarray count]; I ++)
{
OBJ = [oldarray objectatindex: I] Copy];
[Newarray addobject: OBJ];
}
//
Nslog (@ "newarray: % @", newarray );
[Newarray release];
// Quick Enumeration
Nsmutablearray * newarray = [nsmutablearray alloc] init];
Nsarray * oldarray = [nsarray arraywithobjects:
@ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "g", @ "H ", nil];
Nslog (@ "oldarray: % @", oldarray );
For (id obj in oldarray)
{
[Newarray addobject: OBJ];
}
//
Nslog (@ "newarray: % @", newarray );
[Newarray release];
// Deep copy
// Nsmutablearray * newarray = [nsmutablearray alloc] init];
Nsarray * oldarray = [nsarray arraywithobjects:
@ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "g", @ "H ", nil];
Nslog (@ "oldarray: % @", oldarray );
Newarray = (nsmutablearray *) cfpropertylistcreatedeepcopy (kcfallocatordefault, (cfpropertylistref) oldarray, kcfpropertylistmutablecontainers );
Nslog (@ "newarray: % @", newarray );
[Newarray release];
// Copy and sort
Nsmutablearray * newarray = [nsmutablearray alloc] init];
Nsarray * oldarray = [nsarray arraywithobjects:
@ "B", @ "A", @ "E", @ "D", @ "C", @ "F", @ "H", @ "g ", nil];
Nslog (@ "oldarray: % @", oldarray );
Nsenumerator * enumerator;
Enumerator = [oldarray objectenumerator];
Id OBJ;
While (OBJ = [enumerator nextobject])
{
[Newarray addobject: OBJ];
}
[Newarray sortusingselector: @ selector (compare :)];
Nslog (@ "newarray: % @", newarray );
[Newarray release];
/* --------------------------- Split the array ------------------------------*/
// Split the string into an array-componentsseparatedbystring:
Nsstring * string = [nsstring alloc] initwithstring: @ "one, two, three, four"];
Nslog (@ "string: % @", string );
Nsarray * array = [String componentsseparatedbystring: @ ","];
Nslog (@ "array: % @", array );
[String release];
// Merge elements from the array to the string-componentsjoinedbystring:
Nsarray * array = [nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four", nil];
Nsstring * string = [array componentsjoinedbystring: @ ","];
Nslog (@ "string: % @", string );
/*************************************** **************************************** ************
Nsmutablearray *************************************** **************************************** ************/
/* --------------- Allocate capacity to the array ----------------*/
Nsarray * array;
Array = [nsmutablearray arraywithcapacity: 20];
/* -------------- Add the object ---------------- at the end of the array ----------------*/
//-(Void) addobject: (ID) anobject;
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
[Array addobject: @ "four"];
Nslog (@ "array: % @", array );
/* -------------- Delete the object at the specified index in the array ----------------*/
//-(Void) removeobjectatindex: (unsigned) index;
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
[Array removeobjectatindex: 1];
Nslog (@ "array: % @", array );
/* ------------- Array enumeration ---------------*/
//-(Nsenumerator *) objectenumerator; forward and backward
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
Nsenumerator * enumerator;
Enumerator = [array objectenumerator];
Id thingie;
While (thingie = [enumerator nextobject]) {
Nslog (@ "thingie: % @", thingie );
}
//-(Nsenumerator *) reverseobjectenumerator; forward from the back
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
Nsenumerator * enumerator;
Enumerator = [array reverseobjectenumerator];
Id object;
While (Object = [enumerator nextobject])
{
Nslog (@ "Object: % @", object );
}
// Quick Enumeration
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
For (nsstring * string in array)
{
Nslog (@ "string: % @", string );
}
/*************************************** **************************************** ************
Nsdictionary
**************************************** **************************************** ***********/
/* ------------------------------------ Create a dictionary ------------------------------------*/
//-(ID) initwithobjectsandkeys;
Nsdictionary * dictionary = [nsdictionary alloc] initwithobjectsandkeys: @ "one", @ "1", @ "two", @ "2", @ "three", @ "3 ", nil];
Nsstring * string = [dictionary objectforkey: @ "one"];
Nslog (@ "string: % @", string );
Nslog (@ "dictionary: % @", dictionary );
[Dictionary release];
/*************************************** **************************************** ************
Nsmutabledictionary
**************************************** **************************************** ***********/
/* ------------------------------------ Create a variable dictionary ------------------------------------*/
// Create
Nsmutabledictionary * dictionary = [nsmutabledictionary dictionary];
// Add a dictionary
[Dictionary setobject: @ "one" forkey: @ "1"];
[Dictionary setobject: @ "two" forkey: @ "2"];
[Dictionary setobject: @ "three" forkey: @ "3"];
[Dictionary setobject: @ "four" forkey: @ "4"];
Nslog (@ "dictionary: % @", dictionary );
// Delete the specified dictionary
[Dictionary removeobjectforkey: @ "3"];
Nslog (@ "dictionary: % @", dictionary );
/*************************************** **************************************** ************
Nsvalue (packaging any object) **************************************** **************************************** ***********/
/* -------------------------------- Add nsrect to nsarray ------------------------------------*/
// Put nsrect into nsarray
Nsmutablearray * array = [nsmutablearray alloc] init];
Nsvalue * value;
Cgrect rect = cgrectmake (0, 0,320,480 );
Value = [nsvalue valuewithbytes: & rect objctype: @ encode (cgrect)];
[Array addobject: value];
Nslog (@ "array: % @", array );
// Extract from Array
Value = [array objectatindex: 0];
[Value getvalue: & rect];
Nslog (@ "value: % @", value );
/*************************************** **************************************** ************
Search for files with the JPG extension from the directory
**************************************** **************************************** ***********/
Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
Nsstring * home;
Home = @ "../users /";
Nsdirectoryenumerator * direnum;
Direnum = [filemanager enumeratoratpath: Home];
Nsmutablearray * files = [nsmutablearray alloc] init];
// Enumeration
Nsstring * filename;
While (filename = [direnum nextobject]) {
If ([filename pathextension] hassuffix: @ "jpg"]) {
[Files addobject: Filename];
}
}
// Quick Enumeration
// For (nsstring * filename in direnum)
//{
// If ([filename pathextension] isw.tostring: @ "jpg"]) {
// [Files addobject: Filename];
//}
//}
Nslog (@ "files: % @", files );
// Enumeration
Nsenumerator * filenum;
Filenum = [files objectenumerator];
While (filename = [filenum nextobject]) {
Nslog (@ "filename: % @", filename );
}
// Quick Enumeration
// For (ID object in files)
//{
// Nslog (@ "Object: % @", object );
//}