Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50643
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-09 10:52
文章分类

全部博文(34)

文章存档

2015年(23)

2014年(11)

我的朋友

分类: iOS平台

2015-04-15 21:35:50

一.字典


NSDictionary是不可变的,并且不可以放基本类型
不可以相同的一个key对应多个values
可以多个key对应相同的values
下面看一下字典的一些操作:

点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // 字典
  4. //
  5. // Created by 金海洋 on 15-4-14.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"

  10. void fun(){
  11. //字典的初始化
  12.     //NSDictionary *di = [NSDictionary dictionary];
  13.     NSDictionary *di = [NSDictionary dictionaryWithObject:
  14.      @"a" forKey:@"b"];
  15.     
  16.     NSLog(@"%@",di);
  17.     
  18.     //最常用的
  19.     di = [NSDictionary dictionaryWithObjectsAndKeys:
  20.     @"a",@"b",
  21.     @"a1",@"b1",
  22.     @"a2",@"b2",nil];
  23.     
  24.     NSLog(@"%@",di);
  25.     
  26.     NSArray *a1 = [NSArray arrayWithObjects:@"a", @"a1",nil];
  27.     NSArray *b1 = [NSArray arrayWithObjects:@"b", @"b1",nil];
  28.     di = [NSDictionary dictionaryWithObjects:a1 forKeys:b1];
  29.     
  30.     NSLog(@"%@",di);
  31.     
  32. }

  33. void fun1(){
  34. //字典的基本用法
  35.     
  36.     NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
  37.           @"a",@"b",
  38.           @"a1",@"b1",
  39.           @"a2",@"b2",nil];
  40.     
  41.     NSDictionary *di1 = [NSDictionary dictionaryWithObjectsAndKeys:
  42.                         @"a",@"b",
  43.                         @"a1",@"b1",
  44.                         @"a2",@"b2",nil];

  45.     //打印你的键值对
  46.     NSLog(@"%d",di.count);

  47.     //比较键值是否一样
  48.     if([di isEqualToDictionary:di1]){
  49.         NSLog(@"一样");
  50.     
  51.     }
  52.     
  53.     //取值
  54.     id o1 = [di objectForKey:@"b"];
  55.     NSLog(@"b--%@",o1);
  56.     
  57.     NSString *path = @"/Users/jinhaiyang/Desktop/aa";
  58.     //将字典写入文件中
  59.     [di writeToFile:path atomically:YES];
  60.     
  61.     //把文件中的东西读取给字典
  62.     NSDictionary *di2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/jinhaiyang/Desktop/aa"];
  63.     NSLog(@"%@",di2);
  64.     
  65. }

  66. void fun2(){
  67. //字典基本用法2
  68.     
  69.     NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
  70.                         @"a",@"b",
  71.                         @"a",@"b1",
  72.                         @"a2",@"b2",nil];
  73.     
  74.     //返回所有的key
  75.     NSArray *n1 = [di allKeys];
  76.     
  77.     NSLog(@"%@",n1);
  78.     
  79.     //返回对象对应的所有key
  80.     NSArray *n2 = [di allKeysForObject:@"a"];
  81.     NSLog(@"%@",n2);
  82.     
  83.     //返回所有的values
  84.     NSArray *n3 = [di allValues];
  85.     NSLog(@"%@",n3);
  86.     
  87.     //根据多个key取出对应的多个values
  88.     //notFoundMarker:<#(id)#> 如果找不到键值的话,就用他来替代
  89.     NSArray *n4 = [di objectsForKeys: [NSArray arrayWithObjects:@"b1",@"b2", @"af",nil] notFoundMarker:@"ccc"];
  90.     NSLog(@"%@",n4);
  91.     

  92. }

  93. void fun3(){
  94. //遍历字典1
  95.     
  96.     NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
  97.                         @"a",@"b",
  98.                         @"a",@"b1",
  99.                         @"a2",@"b2",nil];
  100.     
  101.     //遍历字典的所有KEY
  102.     for(id key in di){
  103.         
  104.         id v = [di objectForKey:key];
  105.         NSLog(@"%@--%@",key,v);
  106.     
  107.     }

  108. }

  109. void fun4(){
  110. //遍历字典2
  111. //迭代器
  112.     NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
  113.                         @"a",@"b",
  114.                         @"a",@"b1",
  115.                         @"a2",@"b2",nil];
  116.     
  117.     //key迭代器
  118.     NSEnumerator *e = [di keyEnumerator];
  119.     
  120.     id key = nil;
  121.     
  122.     while (key = [e nextObject]) {
  123.         id a = [di objectForKey:key];
  124.         NSLog(@"%@-%@",key,a);
  125.     }
  126.     
  127.     //对象迭代器
  128.     e = [di objectEnumerator];


  129. }

  130. void fun5(){
  131. //字典遍历3
  132. //block
  133.     NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
  134.                         @"a",@"b",
  135.                         @"a",@"b1",
  136.                         @"a2",@"b2",nil];
  137.     
  138.     
  139.     [di enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  140.         NSLog(@"%@-%@",key,obj);
  141.     }];
  142.     

  143. }

  144. void fun6(){
  145. //内存管理

  146.     Student *s1 = [Student StudentWithName:@"ac"];
  147.     Student *s2 = [Student StudentWithName:@"adc"];
  148.     Student *s3 = [Student StudentWithName:@"vc"];
  149.     //对象当做key或者values的话就会做一次retain操作
  150.     NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
  151.                         s1,@"b",
  152.                         s2,@"b1",
  153.                         s3,@"b2",nil];
  154.     
  155.     //当字典被销毁时,里面的Key和values会被release
  156.     

  157. }

  158. int main(int argc, const char * argv[]) {
  159.     @autoreleasepool {
  160.         
  161.         //fun();
  162.         //fun1();
  163.         //fun2();
  164.         //fun3();
  165.         //fun4();
  166.         //fun5();
  167.         fun6();
  168.         
  169.     }
  170.     return 0;
  171. }

二.可变字典


下面看一下代码:

点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // 可变字典
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"

  10. void fun1(){
  11. //基本用法
  12.     
  13.     //创建
  14.     NSMutableDictionary *n1 = [NSMutableDictionary dictionary];
  15.     NSDictionary *n = [NSDictionary dictionaryWithObject:@"11" forKey:@"f"];
  16.     
  17.     Student * s1 = [Student StudentWithName:@"ad"];
  18.     Student * s2 = [Student StudentWithName:@"aa"];
  19.     
  20.     //添加
  21.     //s1 的计数器 会加1
  22.     [n1 setObject:s1 forKey:@"1"];
  23.     //删除所有元素
  24.     [n1 removeAllObjects];
  25.     //删除指定的key对应的元素
  26.     [n1 removeObjectForKey:@"1"];
  27.     //方法和字典的一些方法一样
  28.     
  29.     //添加字典到 现有字典里
  30.     [n1 addEntriesFromDictionary:n ];
  31.     
  32.     NSLog(@"%@",n1);

  33. }


  34. int main(int argc, const char * argv[]) {
  35.     @autoreleasepool {
  36.         
  37.         fun1();
  38.         
  39.     }
  40.     return 0;
  41. }

二.NSNumber

NSNumber可以把基本类型包装成类,但是不能包装结构体
看下代码:

点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // NSNumber
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>

  9. void fun(){

  10.     //包装成一个NSNumber对象
  11.     NSNumber *n = [NSNumber numberWithInt:45];
  12.     NSLog(@"%@",n);
  13.     NSMutableArray *n1 = [NSMutableArray array];
  14.     //添加到数组中
  15.     [n1 addObject:n];
  16.     
  17.     //取出来还是NSNumber对象,不支持自动解包
  18.     NSNumber *n2 = [n1 lastObject];
  19.     //转换成int类型
  20.     int i = [n2 intValue];

  21. }

  22. int main(int argc, const char * argv[]) {
  23.     @autoreleasepool {
  24.         
  25.         fun();
  26.         
  27.         
  28.     }
  29.     return 0;
  30. }

三.NSValue

NSNumber是NSvalue的子类,NSvalue能包装任何类型的数据
看下代码:

点击(此处)折叠或打开

  1. #import <Foundation/Foundation.h>

  2. typedef struct{

  3.     int age;
  4.     int no;
  5.     
  6. } Stu;


  7. void fun(){
  8. //包装一个结构体
  9.     CGPoint p = CGPointMake(4, 2);
  10.     
  11.     NSValue *n1 =[NSValue valueWithPoint:p];
  12.     
  13.     NSMutableArray *n2 = [NSMutableArray array];
  14.     
  15.     [n2 addObject:n1];
  16.     //取出值
  17.     NSValue *n3 = [n2 lastObject];
  18.     
  19.     //取出结构体
  20.     CGPoint p1 = [n3 pointValue];
  21.    
  22.     if( CGPointEqualToPoint(p, p1) ){
  23.         NSLog(@"成功");
  24.     
  25.     }
  26.     
  27. }

  28. void fun1(){

  29.     Stu s={1,22};
  30.     //根据类型产生字符串
  31.     char *type = @encode(Stu);

  32.     NSValue *n1 = [NSValue value:&s withObjCType:type];
  33.     
  34.     NSMutableArray *n2 = [NSMutableArray array];
  35.     
  36.     [n2 addObject:n1];
  37.     
  38.     Stu ss;
  39.     //取出包装好的结构体
  40.     [n1 getValue:&ss];
  41.     
  42.     NSLog(@"%d",ss.age);
  43.     
  44.     //取出包装类型的字符串
  45.     const char *t1 = [n1 objCType];
  46.     
  47.     
  48. }



  49. int main(int argc, const char * argv[]) {
  50.     @autoreleasepool {
  51.         
  52.         //fun();
  53.         fun1();
  54.         
  55.         
  56.     }
  57.     return 0;
  58. }

四.NSNull

作用是,为了能把一个空值放入到对象
代码如下:

点击(此处)折叠或打开

  1. #import <Foundation/Foundation.h>

  2. int main(int argc, const char * argv[]) {
  3.     @autoreleasepool {
  4.         
  5.         //是个单例,只有一个静态方法
  6.         //单例:所有的实例对象都是相同的对象
  7.         NSNull *n = [NSNull null];
  8.         NSNull *n1 = [NSNull null];
  9.         
  10.         if(n == n1)
  11.             NSLog(@"是个单例");
  12.         
  13.         //如果字典里面有null 就不会解析 相当于没有
  14.         
  15.     }
  16.     return 0;
  17. }

五.NSDate

代码如下:

点击(此处)折叠或打开

  1. #import <Foundation/Foundation.h>

  2. void fun(){
  3.     
  4.     //返回的是当前时间
  5.     NSDate *d1 = [NSDate date];
  6.     NSLog(@"%@",d1);
  7.     
  8.     //比当前时间快xxx秒
  9.     d1 = [NSDate dateWithTimeIntervalSinceNow:10];
  10.     NSLog(@"%@",d1);
  11.     
  12.     //从1970年1月1日 开始算的时间
  13.     d1 = [NSDate dateWithTimeIntervalSince1970:10];
  14.     NSLog(@"%@",d1);
  15.     
  16.     //随机返回一个比较遥远的未来时间
  17.     d1 = [NSDate distantFuture];
  18.     NSLog(@"%@",d1);
  19.     
  20.     //随机返回一个比较遥远的过去时间
  21.     d1 = [NSDate distantPast];
  22.     NSLog(@"%@",d1);
  23.     

  24.     
  25. }

  26. void fun1(){

  27.     NSDate *d1 = [NSDate date];
  28.     NSDate *d2 = [NSDate date];
  29.     
  30.     //返回1970 1 1开始返回的毫秒数
  31.     NSTimeInterval ti = [d1 timeIntervalSince1970];
  32.     //和其他时间进行对比
  33.     [d1 timeIntervalSinceDate:d2];
  34.     
  35.     //返回一个比较早的那个时间
  36.     [d1 earlierDate:d2];
  37.     
  38.     //返回一个比较晚的那个时间
  39.     [d1 laterDate:d2];
  40.     
  41.     
  42. }


  43. void fun2(){
  44. //格式化
  45.     NSDate *d1 = [NSDate date];
  46.     NSDateFormatter *d2 = [[[NSDateFormatter alloc]init]autorelease];
  47.     
  48.     //给他格式
  49.     //2000-00-00 00:00:00
  50.     //HH24进制 hh12进制
  51.     d2.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  52.     //设置时区
  53.     d2.locale =[[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]autorelease];
  54.     
  55.     
  56.     //赋值 string---date
  57.     NSString *s=[d2 stringFromDate:d1];
  58.     
  59.     //date -- string
  60.     NSDate *d3 = [d2 dateFromString:@"2015-4-15 09:26:01"];
  61.     
  62.     NSLog(@"%@",s);
  63.     NSLog(@"%@",d3);

  64. }

  65. int main(int argc, const char * argv[]) {
  66.     @autoreleasepool {
  67.         
  68.         //fun();
  69.         //fun1();
  70.         fun2();
  71.         
  72.         
  73.     }
  74.     return 0;
  75. }






阅读(639) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~