[Objective-c] 008_foundation framework of Nsarray and Nsmutablearray

Source: Internet
Author: User

In the Cocoa Foundation, Nsarray and Nsmutablearray are used for an ordered set of objects, the biggest difference between Nsarray and Nsmutablearray classes is that Nsarray is immutable and Nsmutablearray is mutable. They can only store cocoa objects (NSObject objects), if you want to save some raw C data (such as: Int,float,double,bool, etc.), you need to encapsulate these raw C data nsnumber type, their subscript is starting from 0, Here are some common primary operations for the Nsarray and Nsmutablearray classes.

1.NSArray initialization

Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Superdo.horse", @ "Superdo.mount", @ "Superdo.ac", nil];// Initialize with an existing array nsarray *array1 = [Nsarray Arraywitharray:array];

2.NSArray Quick Enumeration

Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Superdo.horse", @ "Superdo.mount", @ "Superdo.ac", Nil];for (NSString *str in array) {    NSLog (@ "%@", str);}

3.NSMutableArray Simple Sorting

Nsmutablearray*array = [[Nsmutablearray alloc] initwithobjects:@ "Superdo.horse", @ "Superdo.mount", @ "SuperDo.AC", nil The elements in the];//array are sorted by string size: [Array sortusingselector: @selector (compare:)]; NSLog (@ "sorted array:%@", array);

4. String---> Nsarray

NSString *string = [[NSString alloc] initwithstring:@ "a| b| c| D "]; NSLog (@ "string:%@", string); Nsarray *array = [string componentsseparatedbystring:@ "|"]; NSLog (@ "array:%@", array);

5.NSArray---> Strings

Nsarray *array = [[Nsarray alloc] initwithobjects:@ "A", @ "B", @ "C", @ "D", nil]; NSString *string = [Array componentsjoinedbystring:@ "|"]; NSLog (@ "string:%@", string);

6. Element manipulation

Insert element Nsmutablearray *array = [Nsmutablearray arraywithobjects:                         @ "One", @ "one", @ "three", nil];[ Array addobject:@ "four"; NSLog (@ "array:%@", array);//delete element [array removeobjectatindex:1]; NSLog (@ "array:%@", array);//enumeration element (formerly Backward) nsenumerator  *enumerator = [Array Objectenumerator];id next;while (next = [ Enumerator Nextobject]) {    NSLog (@ "Object------":%@ ", next);} enumeration element (from backward forward) nsenumerator *enumerator = [Array Reverseobjectenumerator];id object;while (object = [Enumerator Nextobject ] {    NSLog (@ "Object------":%@ ", object);}

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4594178.html

[Objective-c] 008_foundation framework of Nsarray and Nsmutablearray

Related Article

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.