common methods for creating nsdictionary:
+ (ID) dictionary;
+ (ID) dictionarywithobjtct :( ID) object forkey :( id ) Key;
+ (ID) dictionarywithobjectandkeys :( ID) firstobject ,...
+ (ID) dictionarywithdictionary :( nsdictionary *) dict
+ (ID) dictionarywithobjects :( nsarray *) Objects forkeys :( nsarray *) Keys
-(ID) initwithobjectsandkeys :( ID) firstobject ,...
-(ID) initwithdictionary :( nsdictionary *) otherdictionary
-(ID) initwithobjects :( nsarray *) Objects forkeys :( nsarray *) Keys
+ (ID) dictionarywithconentsoffile :( nsstring *) URL
+ (ID) dictionarywithcontentsurl :( nsstring *) path
-(ID) initwithcontentsoffile :( nsstring *) path
-(ID) initwithcontentsofurl :( nsstring *) URL
Common Methods:
// How many key-value pairs
-(Nsuinteger) count;
// Compare two dictionaries
-(Bool) isequaltodictionary :( nsdictionary *) otherdictionary
// Persists an nsdictionary to a file
-(Bool) writetofile :( nsstring *) path atomically :( bool) useauxiliaryfile
// Return all keys
-(Nsarray *) allkeys
// Return all keys corresponding to the object element
-(Nsarray *) allkeysforobject :( ID) anobject
// Return all values
-(Nsarray *) allvalues
// Return value based on key
-(ID) objectforkey :( ID) akey
// Return all values corresponding to keys. If no value exists, use marker instead. Return multiple values based on multiple keys.
// When the corresponding value cannot be found, use marker instead.
-(Nsarray *) objectsforkeys :( nsarray *) Keys notfoundmarker :( ID) marker
Dictionary traversal:
1. Fast traversal:
For (nsstring * key in dict)
2. iterator traversal:
// Key iterator
-(Nsenumerator *) keyenumerator
// Object iterator
-(Nsenumerator *) objectenumerator
3. Block traversal: See the following test demo.Code
4. Dictionary Memory Management:
See the following test DEMO code.
Sort keys (similar to nsarray sorting ):
-(Nsarray *) keyssortedbyvalueusingcomparator :( nscomparator) cmptr
-(Nsarray *) kyessortedbyvaluewitexceptions :( nssortoptions) opts usingcomparator :( nscomparator) cmptr
-(Nsarray *) keyssortedbyvalueusingselector :( SEL) Comparator
The test demo is as follows:
Void dictcreate (){
Nsdictionary * dict = [nsdictionary dictionarywithobject: @ "test" forkey: @ "T"];
Nslog (@ "% @", dict );
}
Void dictuse (){
Nsdictionary * dict = [nsdictionary dictionarywithobjectsandkeys:
@ "A", @ "K1 ",
@ "B", @ "K2 ",
@ "C", @ "K3 ",
Nil];
Nslog (@ "Count = % zi", dict. Count );
// Write to file
Nsstring * Path = @ "/users/gongpb/develop/nsdictionary/dict. xml ";
[Dict writetofile: path atomically: Yes];
Dict = [nsdictionary dictionarywithcontentsoffile: path];
Nslog (@ "dict: % @", dict );
Nsarray * array = [dict objectsforkeys: [nsarray arraywithobjects: @ "K1", @ "K4", nil] notfoundmarker: @ "not found"];
Nslog (@ "find result: % @", array );
}
// Fast Traversal
Void dictfor1 (){
Nsdictionary * dict = [nsdictionary dictionarywithobjectsandkeys:
@ "A", @ "K1 ",
@ "B", @ "K2 ",
@ "C", @ "K3 ",
Nil];
For (ID key in dict ){
ID value = [dict objectforkey: Key];
Nslog (@ "% @ = % @", key, value );
}
}
// Iterator Traversal
Void dictfor2 (){
Nsdictionary * dict = [nsdictionary dictionarywithobjectsandkeys:
@ "A", @ "K1 ",
@ "B", @ "K2 ",
@ "C", @ "K3 ",
Nil];
Nsenumerator * enumer = [dict keyenumerator];
ID key = nil;
While (Key = [enumer nextobject]) {
ID value = [dict objectforkey: Key];
Nslog (@ "% @ = % @", key, value );
}
}
# Pragma mark block Traversal
Void dictfor3 (){
Nsdictionary * dict = [nsdictionary dictionarywithobjectsandkeys:
@ "A", @ "K1 ",
@ "B", @ "K2 ",
@ "C", @ "K3 ",
Nil];
[Dict enumeratekeysandobjectsusingblock: ^ (ID key, Id OBJ, bool * Stop ){
Nslog (@ "% @ = % @", key, OBJ );
}];
}
// Dictionary Memory Management
Void dictmemory (){
Student * stu1 = [STUDENT studentwithname: @ "stu1"];
Student * stu2 = [STUDENT studentwithname: @ "stu1"];
Student * stu3 = [STUDENT studentwithname: @ "stu1"];
// When an object is used as the dictionary key or value, a retain operation will be performed, and the counter will add one
Nsdictionary * dict = [nsdictionary dictionarywithobjectsandkeys:
Stu1, @ "K1 ",
Stu2, @ "K2 ",
Stu3, @ "K3 ",
Nil];
// When the dictionary is destroyed, all the keys and values in the dictionary will have a releas operation, and the counter will be reduced by one.
Nslog (@ "% @", dict );
}
Int main (INT argc, const char * argv [])
{
@ Autoreleasepool {
Nslog (@ "------- dictcreate ------");
// Dictcreate ();
Nslog (@ "------- dictuse ----------");
Dictuse ();
Nslog (@ "------- dictfor1 ---------");
Dictfor1 ();
Nslog (@ "------- dictfor2 ----------");
Dictfor2 ();
Nslog (@ "------- dictfor3 -----------");
Dictfor3 ();
Nslog (@ "------- dictmemory ---------");
Dictmemory ();
}
Return 0;
}
. H file:
# Import <Foundation/Foundation. h>
@ Interface Student: nsobject
@ Property (nonatomic, retain) nsstring * Name;
+ (ID) studentwithname :( nsstring *) Name;
@ End
. M file:
# Import "student. H"
@ Implementation student
+ (ID) studentwithname :( nsstring *) name {
Student * Stu = [[STUDENT alloc] init] autorelease];
Stu. Name = Name;
Return Stu;
}
-(Void) dealloc {
Nslog (@ "Student: % @ is destroyed", _ name );
[Super dealloc];
}
@ end