Summary of Nsarray,nsset,nsdictionary in iOS

Source: Internet
Author: User
Tags bool count hash

Three collection classes to collect Cocoa objects (NSObject objects):

Nsarray for an ordered set of objects (equivalent to an array)

Nsset for object unordered collections

Nsdictionary for key-value mappings

The above three collection classes are immutable (once initialized, they cannot be changed)

The following are the corresponding three variable collection classes (these three variable collection classes are subclasses of the three collection classes above):

Nsmutablearray

Nsmutableset

Nsmutabledictionary

Note: These collection classes can only collect Cocoa objects (Nsojbect objects), and if you want to save some of the original C data (for example, int, float, double, bool, etc.), you need to encapsulate these raw C data into nsnumber types, The NSNumber object is a cocoa object that can be saved in the collection class.

Nsarray

Ordered collection of objects. Immutable. You are cannot add or remove objects to it once it ' s created.

Important methods:

+ (ID) arraywithobjects: (ID) firstobject, ...; Nil terminated

-(int) count;

-(ID) Objectatindex: (int) index; NSString *s1=[[myarray objectatindex:0];

-(void) Makeobjectsperformselector: (SEL) Aselector;

-(Nsarray *) Sortedarrayusingselector: (SEL) Aselector;

-(ID) lastobject; Returns nil if there are no objects in the array (convenient)

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 and 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", @ "two", @ "Three", @ "Four", nil];

Self.dataarray = array;

[Array release];

-(unsigned) count; The number of objects contained in the array;

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

-(ID) Objectatindex: (unsigned int) index; Gets the object at the specified index;

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

------Copy data from one array to another array (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];

Copy and sort

Nsmutablearray *newarray = [[Nsmutablearray alloc] init];

Nsarray *oldarray = [Nsarray arraywithobjects:

Nsmutablearray

mutable version of Nsarray.

-(void) AddObject: (ID) anobject;

-(void) InsertObject: (ID) anobject atindex: (int) index;

-(void) Removeobjectatindex: (int) index;

-(void) removeallobjects;

-----allocates capacity to an 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", @ "two", @ "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", @ "two", @ "Three", nil];

[Array removeobjectatindex:1];

NSLog (@ "array:%@", array);

-----Array Enumeration-----

1,-(Nsenumerator *) Objectenumerator; Back to Front

Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "two", @ "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", @ "two", @ "Three", nil];

Nsenumerator *enumerator;

Enumerator = [array reverseobjectenumerator];

ID object;

while (object = [Enumerator Nextobject]) {

NSLog (@ "object:%@", object);

}

3. Quick Enumeration

Nsmutablearray *array = [Nsmutablearray arraywithobjects:

@ "One", @ "two", @ "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, the object's counter is added 1 after the object is appended

[P1 release];

NSLog (@ "P1 count:%d", [P1 Retaincount]);

Also do array substitution when

[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 exist

[P2 release];

NSLog (@ "P2 count:%d", [P2 retaincount]);

Execute empty array

[Objectsarray removeallobjects];

NSLog (@ "P2 count:%d", [P2 retaincount]);//Output 1, object P2 still exist

[P2 release];

It can be concluded that each time the array operation is performed, the object release is executed, such as the statement in the above comment, to ensure that the memory is not compromised.

Nsset

Unordered collection of objects.

Immutable. You are cannot add or remove objects to it once it ' s created.

Important methods:

+ setwithobjects: (ID) firstobj, ...; Nil terminated

-(int) count;

-(BOOL) Containsobject: (ID) anobject;

-(ID) anyobject;

-(void) Makeobjectsperformselector: (SEL) Aselector;

-(ID) member: (ID) anobject; Uses Isequal:and returns a matching object (if any)

Nsmutableset

mutable version of Nsset.

+ (Nsmutableset *) set;

-(void) AddObject: (ID) anobject;

-(void) Removeobject: (ID) anobject;

-(void) removeallobjects;

-(void) Unionset: (Nsset *) Otherset;

-(void) Minusset: (Nsset *) Otherset;

-(void) Intersectset: (Nsset *) Otherset;

Nsdictionary

Key-value, Key-value, ... A series of key-value pairs.

Key (key) in the entire dictionary is unique, through the key can query its corresponding one or more value (value).

Hash table. Look up objects using a key to get a value.

Immutable. You are cannot add or remove objects to it once it ' s created.

Keys are objects which must implement. Keys are usually nsstring objects.

-(Nsuinteger) Hash &-(BOOL) IsEqual: (NSObject *) obj

Important methods:

+ Dictionarywithobjectsandkeys: (ID) firstobject, ...;

-(int) count;

-(ID) Objectforkey: (ID) key;

-(Nsarray *) AllKeys;

-(Nsarray *) allvalues;

-----Create a dictionary-----

-(ID) Initwithobjectsandkeys;

Nsdictionary *dictionary = [[Nsdictionary alloc] initwithobjectsandkeys:@ "One", @ "1", @ "two", @ "2", @ "Three", @ "3", nil ];

NSString *string = [Dictionary objectforkey:@ "One"];

NSLog (@ "string:%@", string);

NSLog (@ "dictionary:%@", dictionary);

[Dictionary release];

Nsmutabledictionary

mutable version of Nsdictionary.

+ (Nsmutabledictionary *) dictionary;

-(void) SetObject: (ID) anobject Forkey: (ID) key;

-(void) Removeobjectforkey: (ID) key;

-(void) removeallobjects;

-(void) Addentriesfromdictionary: (Nsdictionary *) otherdictionary;

-----Create a variable dictionary-----

Create

Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionary];

Add a dictionary

[Dictionary setobject:@ "One" forkey:@ "1"];

[Dictionary setobject:@ "two" forkey:@ "2"];

[Dictionary setobject:@ "Three" forkey:@ "3"];

[Dictionary setobject:@ "Four" forkey:@ "4"];

NSLog (@ "dictionary:%@", dictionary);

Deletes the specified dictionary

[Dictionary removeobjectforkey:@ "3"];

NSLog (@ "dictionary:%@", dictionary);

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.