常用與建立NSDictionary的方法:
+ (id)dictionary;
+ (id)dictionaryWithObjtct:(id)object forKey:(id <NSCopying>)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
常用方法:
//多少索引值對
- (NSUInteger)count;
//比較兩個字典
- (Bool)isEqualToDictionary:(NSDictionary *)otherDictionary
//將一個NSDictionary持久化到一個檔案中
- (Bool)writeToFile:(NSString *)path atomically:(Bool)useAuxiliaryFile
//返回所有的key
- (NSArray *)allKeys
//返回Object元素對應的所有key
- (NSArray *)allKeysForObject:(id)anObject
//返回所有的value
- (NsArray *)allValues
//根據key返回value
- (id)objectForKey:(id)akey
//返回keys對應的所有value,如果沒有對應的value,用marker代替,根據多個key返回多個value
//當可以找不到對應的value時,用marker代替
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker
字典的遍曆:
1、快速遍曆:
for (NSString *key in dict)
2、迭代器遍曆:
//key迭代器
- (NSEnumerator *)keyEnumerator
//對象迭代器
- (NSEnumerator *)objectEnumerator
3、block遍曆:見下面測試demo代碼
4、字典的記憶體管理:
見下面測試demo代碼
對key排序(用法和NSArray排序類似):
- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr
- (NSArray *)kyesSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator
測試demo如下:
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/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);
}
//快速遍曆
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);
}
}
//迭代器遍曆
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遍曆
void dictFor3(){
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"a",@"k1",
@"b",@"k2",
@"c",@"k3",
nil];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@=%@",key,obj);
}];
}
//字典記憶體管理
void dictMemory(){
Student *stu1 = [Student studentWithName:@"stu1"];
Student *stu2 = [Student studentWithName:@"stu1"];
Student *stu3 = [Student studentWithName:@"stu1"];
//一個對象作為字典的key或者value時,會做一次retain操作,計數器會加一
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
stu1,@"k1",
stu2,@"k2",
stu3,@"k3",
nil];
// 當字典被銷毀時,裡面所有key和value都會有一次releas操作,計數器減一
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檔案:
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic,retain)NSString *name;
+(id)studentWithName:(NSString *)name;
@end
.m檔案:
#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