The Nsarray and Nsmutablearr of Objective-c grammar

Source: Internet
Author: User

An array of objective-c is more powerful than an array of C++,java, and Nsarray saved objects can be different objects. But can only save objects, int, char,double and other basic data types can not be saved directly, you need to convert to an object to join the array.

1, nsarray non-variable group

[Array Count]: the length of the array.
[Array Objectatindex 0]: The ID of the incoming array pin gets the data object.
[Arraywithobjects; ...] : Initializes an assignment to an array object. You can write pointers to arbitrary objects, and you must use nil at the end.

1.1. Creating an array

1. Creating an array

Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "four", nil];

2. The number of objects contained in the array

[Self.dataarray Count]

3. Gets the object at the specified index

[Self.dataarray Objectatindex:2]

4. Copying data from one array to another array (variable-number)

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

5. COPY

 

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

6. 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];

7. Fast 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];


8, cut the fractional group

NSString *string = [[NSString alloc] initwithstring:@ "One,two,three,four"];
NSLog (@ "string:%@", string);
Nsarray *array = [string componentsseparatedbystring:@ ","];
NSLog (@ "array:%@", array);
[String release];

9. Merging elements from arrays to strings

Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "four", nil];
NSString *string = [Array componentsjoinedbystring:@ ","];
NSLog (@ "string:%@", string);

10. Allocating capacity to arrays

Array = [Nsmutablearray arraywithcapacity:20];

11. Adding objects at the end of the array

Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
[Array addobject:@ "four"];
NSLog (@ "array:%@", array);

12. Delete the object at the specified index in the array

Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
[Array removeobjectatindex:1];
NSLog (@ "array:%@", array);

The Nsarray and Nsmutablearr of Objective-c grammar

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.