Objective-C之Foundation框架中的NSDictionary的用法以及它的遍历方法
2013-07-29 21:10
429 查看
常用与创建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
+ (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
相关文章推荐
- Objective-C之foundation框架中的NSString的使用方法、指针基本用法、读取本地和远程文件
- [Objective-C] 009_Foundation框架之NSDictionary与NSMutableDictionary
- Objective-C 【NSDictionary 概念、创建方法、注意事项、遍历问题】
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(3)
- python基本数据类型list,tuple,set,dict用法以及遍历方法
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
- 黑马程序员——Foundation框架——NSDictionary常用方法
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(2)
- NSDictionary创建、引用以及遍历等功能(Objective-C 开发范例)
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(3)
- 黑马程序员——Objective-C Foundation框架中的NSDirctionary类以及NSMutableDirctionary类
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(1)
- Objective-C:Foundation框架-常用类-NSDictionary
- HashMap的一般用法以及遍历方法
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法(1)
- Objective-C Foundation框架实践——NSDictionary(一)
- Objective-C 2.0 with Cocoa Foundation--- 8,类方法以及私有方法