Objective-C: Foundation——NSDictionary、NSMutableDictionary、Block

1.NSDictionary

1.1 由键值组成,关键字不允许重复,值可以重复。
1.2 创建方法
1.3 求键值对个数
1.4 将字典中的所有值转换成数组
1.5 将字典中的所有关键字转换成数组
1.6 通过关键字找到对应的值(3种)
1.7 通过值来找到对应的关键字
1.8 对字典的值进行排序
1.9 遍历(3种)
1.10 字典的文件读写方法
main函数

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建方法
        NSDictionary *dict1=[NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1", @"two",@"2",@"three",@"3",nil]; //标准方法,先写值后写关键字
        NSLog(@"标准创建%@",dict1);

        NSDictionary *dict2=@{@"1":@"one",@"2":@"two",@"3":@"three"}; //优化方法,先写关键字,常用
        NSLog(@"常用方法%@",dict2);

        NSArray *object=@[@"one",@"two",@"three"];
        NSArray *key=@[@"1",@"2",@"3"];
        NSDictionary* dict3=[NSDictionary dictionaryWithObjects:object forKeys:key]; //数组转换成字典
        NSLog(@"数组转换:%@",dict3);

        NSDictionary *dict4=[NSDictionary dictionaryWithDictionary:dict3];//创建副本
        NSLog(@"创建副本:%@",dict4);


        //求键值对个数
        NSLog(@"键值对个数:%lu",dict3.count);

        //获取字典中的所有值,即将字典转换成数组的方法
        object=[dict3 allValues];
        NSLog(@"数组获取字典的值:%@",object);

        //获取字典中的关键字,即将字典转换成数组的方法
        key=[dict3 allKeys];
        NSLog(@"数组获取字典关键字:%@",key);

        //根据一个关键字来查找一个值
        NSString *str=[dict3 objectForKey:@"1"];  //标准方法,求出1所对应的值返回
        NSLog(@"1对应的值%@",str);

        str=dict3[@"1"];  //优化方法,常用
        NSLog(@"优化方法:%@",str);

        //根据多个关键字找多个值
        NSArray *key1=@[@"1",@"4"];
        NSArray *object1=[dict3 objectsForKeys:key1 notFoundMarker:@"没有对应的值"];
        for(id obj in object1)
        {
            NSLog(@"找值:%@",obj);
        }

        //通过值找对应的关键字
        NSArray *key2=[dict3 allKeysForObject:@"three"];
        NSLog(@"%@",key2);

        //对字典中的值进行排序
        NSArray *key3=[dict3 keysSortedByValueUsingSelector:@selector(compare:)];
        for(id obj in key3)
        {
            NSLog(@"%@=%@",obj,dict3[obj]);
        }

        CZStudent *stu1=[CZStudent studentWithName:@"no1" andAge:16];
         CZStudent *stu2=[CZStudent studentWithName:@"no2" andAge:20];
         CZStudent *stu3=[CZStudent studentWithName:@"no3" andAge:19];
        NSDictionary *stu=@{@"1":stu1,@"2":stu2,@"3":stu3};
        NSArray *sorted=[stu keysSortedByValueUsingSelector:@selector(compareAge:)];
        for(id i in sorted)
        {
            NSLog(@"%@=%@",i,stu[i]);
        }


        //遍历
        NSEnumerator *e=[dict3 objectEnumerator]; //枚举值遍历
        while(str=[e nextObject])
        {
            NSLog(@"%@",str);
        }
        e=[dict3 keyEnumerator];  //枚举关键字遍历
        while(str=[e nextObject])
        {
            NSLog(@"%@",str);
        }

        for(id obj in dict3)
        {
            NSLog(@"%@=%@",obj,dict3[obj]);
        }

        //字典的文件读写
        [dict3 writeToFile:@"/Users/tarena/Desktop/dict.plist" atomically:YES];
        NSDictionary *dict5=[NSDictionary dictionaryWithContentsOfFile:@"/Users/tarena/Desktop/dict.plist"];
        NSLog(@"%@",dict5);

    }
    return 0;
}

运行结果

2016-08-30 10:52:07.899 cz_day23_01[1119:46687] 标准创建{
    1 = one;
    2 = two;
    3 = three;
}
2016-08-30 10:52:07.900 cz_day23_01[1119:46687] 常用方法{
    1 = one;
    2 = two;
    3 = three;
}
2016-08-30 10:52:07.900 cz_day23_01[1119:46687] 数组转换:{
    1 = one;
    2 = two;
    3 = three;
}
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 创建副本:{
    1 = one;
    2 = two;
    3 = three;
}
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 键值对个数:3
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 数组获取字典的值:(
    one,
    two,
    three
)
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 数组获取字典关键字:(
    1,
    2,
    3
)
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 1对应的值one
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 优化方法:one
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 找值:one
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 找值:没有对应的值
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] (
    3
)
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 1=one
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 3=three
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 2=two
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 1=name:no1,age:16
2016-08-30 10:52:07.901 cz_day23_01[1119:46687] 3=name:no3,age:19
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 2=name:no2,age:20
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] one
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] two
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] three
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 1
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 2
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 3
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 1=one
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 2=two
2016-08-30 10:52:07.902 cz_day23_01[1119:46687] 3=three
2016-08-30 10:52:07.906 cz_day23_01[1119:46687] {
    1 = one;
    2 = two;
    3 = three;
}
Program ended with exit code: 0

2.NSMutebleDictionary

2.1 可变字典,是NSDictionary的子类
2.2 创建方法(3种)
2.3 添加方法(2种)
2.4 覆盖方法
2.5 删除方法

main函数

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建方法
        NSMutableDictionary *dict1=[NSMutableDictionary dictionary]; //空字典,有意义
        NSMutableDictionary *dict2=[NSMutableDictionary dictionaryWithCapacity:100];  //预估值
        NSMutableDictionary *dict3=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];  //标准方法
        NSMutableDictionary *dict4=@{@"1":@"one",@"2":@"two"};   //退化成不可变字典


        //添加方法
        [dict3 setValue:@"four" forKey:@"4"];
        NSLog(@"%@",dict3);
        NSDictionary *added = @{@"5":@"five",@"6":@"six",@"7":@"seven"};
        [dict3 addEntriesFromDictionary:added];
        NSLog(@"%@",dict3);

        //覆盖
        NSDictionary *dict5=@{@"1":@"one",@"2":@"two",@"3":@"three"};
        NSMutableDictionary *dict6=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaa",@"1",@"bbb",@"2",@"ccc",@"3", nil];
        [dict6 setDictionary:dict5];
        NSLog(@"%@",dict6);

        //删除方法
        [dict3 removeObjectForKey:@"1"];  //删除指定关键字对应的键值对
        NSLog(@"%@",dict3);
        NSArray *del =@[@"3",@"5"];
        [dict3 removeObjectsForKeys:del];
        NSLog(@"%@",dict3);
        [dict3 removeAllObjects];
        NSLog(@"%lu",dict3.count);

    }
    return 0;
}

运行结果

2016-08-30 11:55:45.041 cz_day23_03[1325:65844] {
    1 = one;
    2 = two;
    3 = three;
    4 = four;
}
2016-08-30 11:55:45.041 cz_day23_03[1325:65844] {
    1 = one;
    2 = two;
    3 = three;
    4 = four;
    5 = five;
    6 = six;
    7 = seven;
}
2016-08-30 11:55:45.042 cz_day23_03[1325:65844] {
    1 = one;
    2 = two;
    3 = three;
}
2016-08-30 11:55:45.042 cz_day23_03[1325:65844] {
    2 = two;
    3 = three;
    4 = four;
    5 = five;
    6 = six;
    7 = seven;
}
2016-08-30 11:55:45.042 cz_day23_03[1325:65844] {
    2 = two;
    4 = four;
    6 = six;
    7 = seven;
}
2016-08-30 11:55:45.042 cz_day23_03[1325:65844] 0
Program ended with exit code: 0

3.Block

3.1 是一种新的数据类型,用于定义变量
3.2 Block类型的变量中存储的数据是一段程序代码
3.3 语法
3.4 Block与全局变量
3.5 Block与局部变量
3.6 Block在自定义类中的使用

建立一个MyClass类
.h文件

#import <Foundation/Foundation.h>

typedef void(^myBlock2)();
typedef double(^myBlock3)(int,int);


@interface CZMyClass : NSObject
-(void)method;
-(void)method:(myBlock2)b;
-(void)method2:(myBlock3)b;
-(void)method3:(myBlock3)b andX:(int)x andY:(int)y;
-(myBlock2)getBlock;
@property(strong) myBlock2 b;
@end

.m文件

#import "CZMyClass.h"

@implementation CZMyClass
-(void)method
{
    NSLog(@"method被执行了");
}
-(void)method:(myBlock2)b
{
    NSLog(@"Block做形参,进行调用结果是:");
    b(); //回调
}
-(void)method2:(myBlock3)b
{
    NSLog(@"%lg",b(3,5));
}
-(void)method3:(myBlock3)b andX:(int)x andY:(int)y
{
    NSLog(@"%lg",b(x,y));
}
-(myBlock2)getBlock
{
    return ^void(void) {
        NSLog(@"Block做方法返回值");};
}
@end

main函数

#import <Foundation/Foundation.h>
#import "CZMyClass.h"
int g_i=10;
void fun()
{
    NSLog(@"函数被执行了");

}

void(^myBlock)(void) = ^void(void)
{
    NSLog(@"Block被执行了");
};

double(^myBlock1)(int,int)=^double(int a,int b)
{
    return a+b;
};

typedef void(^BlockType)(void);
typedef double(^BlockType1)(int,int);


void fa(BlockType1 a)
{
    NSLog(@"%lg",a(3,5));
}



int main(int argc, const char * argv[]) {
    @autoreleasepool {


        //语法
        fun();
        myBlock();
        void(*pf)()=fun;
        pf();
        NSLog(@"%lg",myBlock1(2,3));

        BlockType b1;
        b1=^void(void)
        {
            NSLog(@"用Block类型定义的变量");
        };
        b1();

        BlockType1 b2=^double(int a,int b)
        {
            return a+b;
        };
        NSLog(@"%lg",b2(5,6));

        fa( ^double(int a,int b) {return a*b;} );


        //Block与全局变量
        BlockType1 b3=^double(int a,int b)
        {
            a *=g_i;  //全局变量可用被访问
            g_i=20;  //全局变量可用被赋值
            return a+b;
        };
        NSLog(@"%lg",b3(2,3));
        NSLog(@"全局变量:%d",g_i);


        //Block与局部变量
        int i2=30;
        __block int i3=50;
        BlockType b4=^void(void)
        {
            NSLog(@"Bolck访问局部变量:%d",i2); //局部变量可用在Block中被访问;
         //   i2=40; ,局部变量不能在Block中赋值
            i3=60;  //加上 __block后,局部变量可用在Block中被赋值
        };
        b4();
        NSLog(@"加上关键字__block的局部变量%d",i3);


        //Block在自定义类中的使用
        CZMyClass *c=[[CZMyClass alloc]init];
        [c method];
        [c method:^void(void)
        {
            NSLog(@"随意吧");

        }];
         [c method:^void(void)
        {
            NSLog(@"不随意");
        }];    //method实现多态

        [c method2:^double(int a, int b)
        {
            return a+b;
        }];

        [c method3:^double(int x, int y)
         {
             return x*y;
         } andX:3 andY:5];

        [c getBlock]();

        c.b=^void(void)
        {
            NSLog(@" Block做属性");
        };
        c.b();


        //用Block排序
        NSNumber *a1=[NSNumber numberWithInt:10];
        NSNumber *a2=[NSNumber numberWithInt:8];
        NSNumber *a3=[NSNumber numberWithInt:20];
        NSNumber *a4=[NSNumber numberWithInt:2];
        NSNumber *a5=[NSNumber numberWithInt:15];

        NSArray *num=@[a1,a2,a3,a4,a5];
        NSArray *sorted=[num sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2)
                         {
                             return [obj1 compare:obj2];   //obj1,obj2调换就是升序
                         }];
        NSLog(@"%@",sorted);
        NSArray *str =@[@"one",@"two",@"three"];
        sorted=[str sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2)
                {
                    return [obj1 compare:obj2];
                }];
        NSLog(@"%@",sorted);


        //Block与字典
        NSDictionary *dict=@{@"1":a1,@"2":a2,@"3":a3,@"4":a4};
        sorted = [dict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2)
                  {
                      return [obj1 compare:obj2];
                  }];
        for(id obj in sorted)
        {
            NSLog(@"%@=%@",obj,dict[obj]);
        }

        NSLog(@"---------------");

        NSDictionary *dict2=@{@"1":@"one",@"2":@"two",@"3":@"three"};
        sorted =[dict2 keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2)
                 {
                     return [obj1 compare:obj2];
                 }];

        for(id obj in sorted)
        {
            NSLog(@"%@=%@",obj,dict2[obj]);
        }


    }
    return 0;
}

运行结果

2016-08-30 16:57:55.646 cz_day23_04[2006:191654] 函数被执行了
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] Block被执行了
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 函数被执行了
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 5
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 用Block类型定义的变量
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 11
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 15
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 23
2016-08-30 16:57:55.647 cz_day23_04[2006:191654] 全局变量:20
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] Bolck访问局部变量:30
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] 加上关键字__block的局部变量60
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] method被执行了
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] Block做形参,进行调用结果是:
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] 随意吧
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] Block做形参,进行调用结果是:
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] 不随意
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] 8
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] 15
2016-08-30 16:57:55.648 cz_day23_04[2006:191654] Block做方法返回值
2016-08-30 16:57:55.648 cz_day23_04[2006:191654]  Block做属性
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] (
    2,
    8,
    10,
    15,
    20
)
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] (
    one,
    three,
    two
)
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 4=2
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 2=8
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 1=10
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 3=20
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] ---------------
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 1=one
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 3=three
2016-08-30 16:57:55.649 cz_day23_04[2006:191654] 2=two
Program ended with exit code: 0

练习

#import <Foundation/Foundation.h>
#import "CZStudent.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CZStudent *s1=[CZStudent studentWithName:@"cz" andAge:15];
        CZStudent *s2=[CZStudent studentWithName:@"sx" andAge:20];
        CZStudent *s3=[CZStudent studentWithName:@"hh" andAge:22];
        CZStudent *s4=[CZStudent studentWithName:@"dd" andAge:14];
        CZStudent *s5=[CZStudent studentWithName:@"as" andAge:18];
        NSArray *array=@[s1,s2,s3,s4,s5];
        NSDictionary *dict=@{@"1":s1,@"2":s2,@"3":s3,@"4":s4,@"5":s5};
        NSArray *sorted1=[array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2)
                         {
                             return [[obj1 name] compare:[obj2 name]];
                         }];
        NSArray *sorted2=[dict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2)
                          {
                              NSNumber *a1=[NSNumber numberWithInt:[obj1 age]];
                              NSNumber *a2=[NSNumber numberWithInt:[obj2 age]];
                              return [a1 compare:a2];
                          }];
        for(id obj in sorted1)
        {
            NSLog(@"%@",obj);
        }

        for(id obj in sorted2)
        {
            NSLog(@"%@=%@",obj,dict[obj]);
        }




    }
    return 0;
}

运行结果

2016-08-30 17:51:10.850 cz_day23_每日一练[2192:202408] name:as,age:18
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] name:cz,age:15
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] name:dd,age:14
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] name:hh,age:22
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] name:sx,age:20
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] 4=name:dd,age:14
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] 1=name:cz,age:15
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] 5=name:as,age:18
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] 2=name:sx,age:20
2016-08-30 17:51:10.851 cz_day23_每日一练[2192:202408] 3=name:hh,age:22
Program ended with exit code: 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值