Common nsarray and nsmutablearray Methods

Source: Internet
Author: User

/* Initialization method:
1. init returns an empty array
2. initwitharray initializes from an existing array
3. initwithcontentsoffile // load from plist File
4. initwithcontentsofurl // obtain from the network address
5. initwithobject is initialized with an object
6. initwithobjects is initialized from multiple objects.

Self. thedataarray = [[nsmutablearray alloc] initwithcapacity: 5]; // specifies that five elements are initialized.

*/

/* Print the first element:
Nslog (@ "the object is: % @", [thedataarray firstobjectcommonwitharray: thedataarray]);
*/
/* Print the last element:
Nslog (@ "the object is: % @", [thedataarray lastobject]);
*/
/* Enumerate all elements. Method 1:
For (nsstring * thestr in thedataarray ){
Nslog (@ "% @:", thestr );
}
*/
/* Enumerate all elements. Method 2:
For (INT I = 0, I <[thedataarray count], I ++ ){
// Nslog (@ "% @:", [thedataarray objectatindex: I]);
}
*/
/* Enumerate all elements. Method 3: Use the enumerator:
Nsenumerator * enumerator = [thedataarray objectenumerator];
Id OBJ;
While (OBJ = [enumerator nextobject]) {
Nslog (@ "% @", OBJ );
}
*/

/* // Add an element
[Thedataarray addobject: @ "this is the newly added element"]; // Add it from the end
*/

/* // Insert an element from the specified index
[Thedataarray insertobject: @ "this is inerted object" atindex: 0]; // insert to the front of the specified index
*/


/* // Modify. Update Element
[Thedataarray replaceobjectatindex: 0 withobject: @ "New OBJ"]; // modify the specified index
*/

/* // Determine whether the array contains an object

If ([thedataarray containsobject: @ "selectiont"]) {
Nslog (@ "the object selection is contained in array ");
}
Else {
Nslog (@ "not contain ");
}
*/
/* // Obtain the element index
Nslog (@ "The idx is: % I", [thedataarray indexofobject: @ "selection"]);
*/

/* // Sort array elements

Method 1:




Nsarray * thetemparr = [[nsarray alloc] initwitharray: [thedataarray
Sortedarrayusingselector: @ selector (caseinsensitivecompare :)];
*/
// [Thedataarray sortedarrayusingselector: @ selector (caseinsensitivecompare :)]; // or @ selector (compare :)

/* // Sort array elements
Method 2:


Nslog (@ "before sorted array: % @", thedataarray );
Nscountedset * cset = [[nscountedset alloc] initwitharray: thedataarray];

Nsarray * thetemparr = [[nsarray alloc] initwitharray: [[cset allobjects] sortedarrayusingselector: @ selector (compare :)];

Nslog (@ "after sorted array: % @", thetemparr );
*/
/* // Sort array objectsNssortdescriptor
Nssortdescriptor * sortdescriptor = [[nssortdescriptor alloc] initwithkey: @ "name" ascending: Yes]; // @ "name" is the object property
[Thedataarray sortusingdescriptors: [nsarray arraywithobject: sortdescriptor]; // return the sorted Array

// You can also use the custom method: [thedataarray sortusingselector: @ selector (custom method :)];
-(Nsinteger) custommethod :( someobject *) otherobj {
Nscompareresult compareresult = [self. Name compare: otherobj. Name];
If (compareresult = nsorderedsame) return 0;
If (compareresult = nsorderedascending) return 1;
Else return-1;
}
*/

/* // Object array filtering (nspredicate)

Nsarray * keyarray = [[nsarray alloce] initwtihobjects: @ "A", @ "B", nil];
For (nsstring * key in keyarray)
{
// Define the rule:

Nspredicate * apredicate = [nspredicate predicatewithformat: @ "self. Name
Beginswith [c] % @ ", key]; // self. Name is an object attribute, beginswith [c] % @", key
Indicates selecting objects whose names start with key.
// Or:
Nspredicate * apredicate = [nspredicate
Predicatewithformat: @ "self. name contains [c] % @", key]; // contains [c]
% @ ", Key indicates selecting objects whose names contain keys
// Or:
Nsinteger age = 20;
Nspredicate * apredicate = [nspredicate predicatewithformat: @ "self. age> % I", age]; // indicates selecting objects whose age is greater than 20

// Or:
Nsstring * matchstr = @ "El *";
Nspredicate * apredicate = [nspredicate predicatewithformat: @ "self. name like % @", matchstr]; // indicates that the name contains objects similar to the string matchstr.

Nsarray * newarr = [thedataarray filteredarrayusingpredicate: apredicate];
[Thedatadict setvalue: newarr forkey: Key];
}

*/

/* // Delete an array element
Nsmutablearray * temparray = [[nsmutablearray alloc] initwithobjects: @ "one", @ "tow", @ "threr", nil];
[Temparray removeobjectatindex: 0]; // remove from the specified index
[Temparray removeallobjects]; // removes all elements.
[Temparray removelastobject]; // removes the last element.
[Temparray removeobjectsinarray: newarray]; // removes elements from the specified array
[Temparray removeobjectsatindexes: nsindexset * _ strong)]; // remove from the selected index

*/

Reprinted address http://1058813598.diandian.com/post/2011-12-24/10271590

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.