设为首页 加入收藏

TOP

Objective-c 字典对象(一)
2017-10-12 18:17:07 】 浏览:9021
Tags:Objective-c 字典 对象

  oc 中的 NSDictionary 的作用同 java 中的字典类相同,提供了 “键-值”对的组合。比如,是用字典类实现对学生姓名和学号的存放,编号是一个键(唯一性),姓名是值。它的方法有:

  

 

  下面通过例子来具体说明它的用法:

   1)构建字典

  

 1 #import <Foundation/Foundation.h>
 2 
 3 int main(int argc , const char *argv[]){
 4     @autoreleasepool{
 5         NSDictionary *dicti1 = [NSDictionary     dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil];
 6         NSEnumerator *keynum = [dicti1 keyEnumerator];
 7         for(NSString *key in keynum)
 8             NSLog(@"key = %@ , value = %@",key,[dicti1 objectForKey:key]);
 9         
10         NSLog(@"----------------------------");
11         NSDictionary *dicti2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil];
12         keynum = [dicti2 keyEnumerator];
13         for(NSString *key in keynum)
14             NSLog(@"key = %@ , value = %@",key,[dicti2 objectForKey:key]);
15         
16         NSLog(@"----------------------------");
17         NSDictionary *dicti3 = @{@"1":@"zhangsan",@"2":@"lisi",@"3":@"wangwu"};
18         keynum = [dicti3 keyEnumerator];
19         for(NSString *key in keynum)
20             NSLog(@"key = %@  , value = %@",key,[dicti3 objectForKey:key]);
21         
22         NSLog(@"------------------------------");
23         NSArray *keyarray = @[@"1",@"2",@"3"];
24         NSArray *valuearray = @[@"zhangsan",@"lisi",@"wangwu"];
25         NSDictionary *dicti4 = [NSDictionary dictionaryWithObjects:valuearray forKeys:keyarray];
26         keynum = [dicti4 keyEnumerator];
27         for(NSString *key in keynum)
28             NSLog(@"key = %@ , value = %@",key,[dicti4 objectForKey:key]);
29         
30     }
31     return 0;
32 }

   

  2)字典的遍历

  

 1 #import <Foundation/Foundation.h>
 2 
 3 int main(int argc , const char *argv[]){
 4     @autoreleasepool {
 5         NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil];
 6         NSEnumerator *number = [dicti keyEnumerator];
 7         for(NSString *key in number)
 8             NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]);
 9         
10         NSLog(@"----------------------------");
11         NSArray *keyarray = [dicti allKeys];
12         for(NSString *key in keyarray)
13             NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]);
14         
15         NSLog(@"-----------------------------");
16         for(NSString *key in  dicti)
17             NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]);
18         
19         NSLog(@"-----------------------------");
20         [dicti enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
21             NSLog(@"key = %@  ,value = %@",key,obj);
22         }];
23     }
    return 0;
24 }

3)  文件的读取

 1 #import <Foundation/Foundation.h>
 2 
 3 int main(int argc , const char *argv[]){
 4     @autoreleasepool {
 5         NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangnwu",@"3",nil];
 6         
 7         NSString *filename = @"dicti.txt&quo
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS--UIAlertView与UIAlertContro.. 下一篇iOS基本数据库存储方式 - CoreData

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目