Objective-C之Foundation框架中的NSDictionary的用法以及它的遍历方法

常用与创建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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值