Nsdictionary and nsmutabledictionary

Source: Internet
Author: User
2) nsnull

Nsnull is probably the simplest class in cocoa. There is only one method.

+ (Nsnull *) NULL;

You can add it to the collection in this way.

[Contact setobject: [nsnull null]

Forkey: @ "home fax machine"];

Access:

Id homefax;

Homefax = [contact objectforkey: @ "home fax machine"];

If (homefax = [nsnull null]) {

//... No fax machine. Rats.

}

// [Nsnull null] always returns the same value, so you can use "=" to compare this value with other values ......

22. nsdictionary and nsmutabledictionary

A) nsdictionary

A dictionary is a set of keywords and their definitions. It is also used as a hash or associated array. It uses an optimized storage method for key queries.

1) creation method: Use dictionarywithobjectsandkeys to create a dictionary.

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

Usage:

Tire * T1 = [Tire new];

Tire * t2 = [Tire new];

Tire * T3 = [Tire new];

Tire * t4 = [Tire new];

Nsdictionary * tires;

Tires = [nsdictionary dictionarywithobjectsandkeys:

T1, @ "front-left", T2, @ "front-Right ",

T3, @ "back-left", T4, @ "back-right", nil];

2) Common Methods

-(ID) objectforkey: (ID) akey;

Usage:

Tire * tire = [Tires objectforkey: @ "back-right"]; // If not, the nil value is returned.

B) nsmutabledictionary

1) creation method:

You can send a dictionary message to a class nsmutabledictionary.

You can also use the function + (ID) dictionarywithcapacity: (unsigned INT) numitems;

2) Common Methods

You can use setobject: forkey: to add an element to the dictionary:

-(Void) setobject: (ID) anobject forkey: (ID) akey;

-(Void) removeobjectforkey: (ID) akey;

Usage:

Nsmutabledictionary * tires;

Tires = [nsmutabledictionary dictionary];

[TIRES setobject: T1 forkey: @ "front-left"];

[TIRES setobject: T2 forkey: @ "front-Right"];

[TIRES setobject: T3 forkey: @ "back-left"];

[TIRES setobject: T4 forkey: @ "back-right"];

// If it already exists, it will replace the original value with the new value

[TIRES removeobjectforkey: @ "back-left"];

23. Do not create nsstring, nsarray, or nsdictionary subclasses, because in cocoa, many classes are actually implemented in the form of class clusters, that is, they are a group of implementation-related classes hidden under common interfaces.

24. Foundation instance // find files

A) Use enumeration Traversal

Int main (INT argc, const char * argv [])

{

NSAID utoreleasepool * pool;

Pool = [[NSAID utoreleasepool alloc] init]; // Automatically releases the pool.

Nsfilemanager * manager; // many classes in cocoa are single-instance architectures, that is, only one instance is required. you really only need one file manager.

Manager = [nsfilemanager defaultmanager]; // The defaultmanager method creates an nsfilemanager object.

Nsstring * home;

Home = [@"~ "Stringbyexpandingtildeinpath]; // The stringbyexpandingtildeinpath method can be set ~ Replace with the current user's home directory

Nsdirectoryenumerator * direnum; // subclass of nsenumerator

Direnum = [Manager enumeratoratpath: Home]; // create an enumeration Condition

Nsmutablearray * files;

Files = [nsmutablearray arraywithcapacity: 42]; // store the search results as files

Nsstring * filename;

While (filename = [direnum nextobject]) {// when you call nextobject, the system will return another path of one file in the directory, or you can search for subdirectories.

If ([[filename pathextension] // the extension of the pathextension output file (remove the preceding vertex .)

Isequalto: @ "jpg"]) {

[Files addobject: Filename];

}

}

Nsenumerator * fileenum;

Fileenum = [files objectenumerator];

While (filename = [fileenum nextobject]) {

Nslog (@ "% @", filename );

}

[Pool drain];

Return (0 );

} // Main

B) Use Quick Traversal

Int main (INT argc, const char * argv []) {

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Nsfilemanager * manager;

Manager = [nsfilemanager defamanager manager];

Nsstring * home;

Home = [@"~ "Stringbyexpandingtildeinpath];

Nsmutablearray * files;

Files = [nsmutablearray arraywithcapacity: 42];

For (nsstring * filename

In [Manager enumeratoratpath: Home]) {

If ([[filename pathextension]

Isequalto: @ "jpg"]) {

[Files addobject: Filename];

}

}

For (nsstring * filename in files ){

Nslog (@ "% @", filename );

}

Berwinzheng

19:35:36

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.