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