Nsarray and variable array nsmutablearray differences and usages of non-variable groups

Source: Internet
Author: User


Nsarray and variable array nsmutablearray differences and usages of non-variable groups

The following is a related code implementation:


#import <Foundation/Foundation.h>

#import "Person.h"

/*

Nsarray: Non-variable group


Nsmutablearray: Variable Array
*/


int main ()
{
@[] Create only immutable groups Nsarray
/* Wrong wording
Nsmutablearray *array = @[@ "Jack" @ "Rose"];

[Array addobject:@ "Jim"];
*/


Nsarray *array = @[@ "Jack" @ "Rose"];

return 0;
}


Basic use of variable groups
void Use3 ()
{
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "Rose", @ "Jim", Nil];

adding elements
[Array Addobject:[[person alloc] init];

[Array addobject:@ "Jack"];

Delete Element
[Array removeallobjects];
Deletes the specified object
[Array removeobject:@ "Jack"];
[Array removeobjectatindex:0];

Wrong wording
[Array Addobject:nil];


NSLog (@ "%@", array);

NSLog (@ "%ld", Array.count);
}


Iterating through an array
void Use2 ()
{
Person *p = [[Person alloc] init];

Nsarray *array = @[p, @ "Rose", @ "Jack"];

for (int i = 0; i<array.count; i++)
//    {
NSLog (@ "%@", Array[i]);
//    }

ID obj represents each element in an array
int i = 0;
for (ID obj in array)
//    {
Find the position of the obj element in the array
Nsuinteger i = [array indexofobject:obj];
//
NSLog (@ "%ld-%@", I, obj);
i++;
//
if (i==1)
//        {
Break
//        }
//    }

[Array Enumerateobjectsusingblock:

Each time you traverse to an element, the block is called
and the current element and index position are passed as parameters to block
^ (ID obj, Nsuinteger idx, BOOL *stop)
{
NSLog (@ "%ld-%@", idx, obj);


if (idx = = 0)
{
Stop traversal
*stop = YES;
}

}];


void ^ (Myblock) (ID, Nsuinteger, bool *) = ^ (Id obj, Nsuinteger idx, BOOL *stop)
//    {
NSLog (@ "%ld-%@", idx, obj);
//
//
if (idx = = 0)
//        {
Stop traversal
*stop = YES;
//        }
//    };
//
for (int i = 0; i<array.count; i++)
//    {
Used to mark whether or not to stop traversal
BOOL isstop = NO;
//
remove element
ID obj = Array[i];
//
Myblock (obj, I, &isstop);
//
//
if (isstop)
//        {
Break
//        }
//    }


}


void use ()
{
/*
int a = 5;

int ages[10] = {1, 90, 89, 17};

Person *p = [[Person alloc] init];
Person *persons[5] = {p, [[Person alloc] init]};
*/

OC Array cannot hold nil value
OC arrays can only hold OC objects, cannot hold non-OC object types, such as int, struct, enum, etc.

This array is always an empty array
Nsarray *array = [Nsarray array];


/*
Creation of 1.NSArray
*/
Nsarray *array2 = [Nsarray arraywithobject:@ "Jack"];

Nil is the tag at the end of the array element
Nsarray *array3 = [Nsarray arraywithobjects:@ "Jack" @ "Rose", nil];
[Array2 Count];

Nsarray *array4 = [Nsarray arraywithobjects:@ "Jack", @ "Rose", @ "4324324", nil];

Quickly create a Nsarray object
Nsarray *array4 = @[@ "Jack" @ "Rose" @ "4324324"];


/*
Number of elements in 2.NSArray
*/
NSLog (@ "%ld", Array3.count);


/*
Access to elements in 3.NSArray
*/
NSLog (@ "%@", [Array3 objectatindex:1]);

ARRAY3[1];
NSLog (@ "%@", array3[0]);
}

Nsarray and variable array nsmutablearray differences and usages of non-variable groups

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.