Array basic operations nsarray/nsmutablearray

Source: Internet
Author: User

From: http://hi.baidu.com/aidfan/blog/item/656fbd45880366288694736b.html

 

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 );

//}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.