Immutable groups and mutable arrays

Source: Internet
Author: User

1. Features of the array:

1) can store multiple data;

2) The type of multiple data stored is the same;

3) The data stored in the array is very easy to manage;

2. When to use arrays

When we have more than one type of data, and the meaning of the data is the same, you need to store it, you can use the array;

=================== Nsarray ====================

Ordered collection of objects. Immutable (Cannot add or remove objects to it once it ' s created)
Important methods:
+ (ID) arraywithobjects: (ID) firstobject, ...; Nil terminated
-(int) count; Get the number of objects in the array
-(ID) Objectatindex: (int) index; Get the object indexed as I

-(BOOL) Containsobject: (ID) anobject; When AnObject appears in the array, it returns Yes (actually judged by the IsEqual: method)

-(unsigned) Indexofobject: (ID) anobject; Finds the anobject in the array and returns its minimum index value. No return nsnotfound found.

-(void) Makeobjectsperformselector: (SEL) Aselector;

-(Nsarray *) Sortedarrayusingselector: (SEL) Aselector;
-(ID) lastobject; Gets the last object in the array. If no object exists in the array, nil is returned
Note:
Class method Arraywithobjects can create an autoreleased nsarray of the items. For example
@implementation MyObject
-(Nsarray *) Coolcats {
return [Nsarray arraywithobjects:@ "Steve", @ "Ankush", @ "Sean", nil];
}
@end
Other convenient create with methods (all return autoreleased objects):
[NSString stringwithformat:@ "meaning of%@ is%d", @ "Life", 42];
[Nsdictionary Dictionarywithobjectsandkeys:ankush, @ "TA", Janestudent, @ "Student", nil];
[Nsarray arraywithcontentsoffile: (NSString *) path];
-----Create an array-----
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "four", nil];

Self.dataarray = array;
[Array release];

NSLog (@ "Self.dataarray count is:%d", [Self.dataarray Count]);

NSLog (@ "Self.dataarray index 2 is:%@", [Self.dataarray objectatindex:2]);

------Copy data from one array to another (variable-level)-------

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

Copy

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

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

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

=================== Nsmutablearray ====================
Mutable version of Nsarray.
-(void) AddObject: (ID) anobject; Adding AnObject at the end of the array, adding nil is illegal.
-(void) Addobjectsfromarray: (Nsarray *) Otherarray; At the end of the array, the objects in the Otherarray are added in sequence.

-(void) InsertObject: (ID) anobject atindex: (int) index; Inserts a anobject at index point and, if index is occupied, moves the subsequent object backward.

-(void) Removeobjectatindex: (int) index; Delete the object at index, and the subsequent objects move forward in turn.

-(void) Removeobject: (ID) anobject; Delete all objects equal to AnObject, and also use isequal: As the equality comparison method.
-(void) removeallobjects;

Note: We cannot add nil to the array. But sometimes we really want to add an empty object to the array, and we can use Nsnull to do it. Such as:

[MyArray addobject:[nsnull Null]];

-----allocates capacity to the array-----
Nsarray *array;
Array = [Nsmutablearray arraywithcapacity:20];

-----Add an object at the end of the array-----
-(void) AddObject: (ID) anobject;
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
[Array addobject:@ "four"];
NSLog (@ "array:%@", array);

-----deletes the object at the specified index in the array-----
-(void) Removeobjectatindex: (unsigned) index;
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
[Array removeobjectatindex:1];
NSLog (@ "array:%@", array);

-----Array Enumeration-----
1,-(Nsenumerator *) Objectenumerator; Front and back
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
Nsenumerator *enumerator;
Enumerator = [array objectenumerator];

ID thingie;
while (thingie = [Enumerator nextobject]) {
NSLog (@ "thingie:%@", thingie);
}
2,-(Nsenumerator *) Reverseobjectenumerator; From the back forward
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
Nsenumerator *enumerator;
Enumerator = [array reverseobjectenumerator];

ID object;
while (object = [Enumerator Nextobject]) {
NSLog (@ "object:%@", object);
}
3. Fast Enumeration
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
For (NSString *string in array) {
NSLog (@ "string:%@", string);
}

-----Nsvalue (Wrapping any object)-----
Put the nsrect into the Nsarray
Nsmutablearray *array = [[Nsmutablearray alloc] init];
Nsvalue *value;
CGRect rect = CGRectMake (0, 0, 320, 480);
Value = [Nsvalue valuewithbytes:&rect objctype: @encode (CGRect)];
[Array Addobject:value];
NSLog (@ "array:%@", array);
Extracting from array
Value = [array objectatindex:0];
[Value getvalue:&rect];
NSLog (@ "value:%@", value);

----★ Use Nsmutablearray to prevent memory leaks ★------
nsobject* P1 = [[NSObject alloc] init];
nsobject* P2 = [[NSObject alloc] init];
nsmutablearray* Objectsarray = [[Nsmutablearray alloc] init];

[Objectsarray ADDOBJECT:P1];
NSLog (@ "P1 count:%d", [P1 retaincount]);//Output 2, that is, after executing an Append object, the object's counter is also added 1
[P1 release];
NSLog (@ "P1 count:%d", [P1 Retaincount]);

When you do the same array substitution
[Objectsarray replaceobjectatindex:0 WITHOBJECT:P2];
NSLog (@ "P2 count:%d", [P2 retaincount]);//Output 2, also 2
NSLog (@ "P1 count:%d", [P1 retaincount]);//Output 1, object P1 still exists
[P2 release];
NSLog (@ "P2 count:%d", [P2 retaincount]);

Execute empty array
[Objectsarray removeallobjects];
NSLog (@ "P2 count:%d", [P2 retaincount]);//Output 1, object P2 still exists
[P2 release];

It follows that each time the above array operation is performed, the object release, such as the statement in the comments above, is performed to ensure that the memory is not compromised.

Immutable groups and mutable arrays

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.