zoukankan      html  css  js  c++  java
  • OC Foundation框架—集合

    Foundation框架—集合

    一、NSArray和NSMutableArray

    (一)NSArray不可变数组

    (1)NSArray的基本介绍

    NSArray是OC中使用的数组,是面向对象的,以面向对象的形式操纵对象,是不可变数组。

    C语言数组有一个缺点即数组中只能存放同种数据类型的元素。

    OC数组只能存放OC对象,不能存放非OC对象,如int,结构体和枚举等

    OC数组实际是指针数组-oc对象是用指针指向的,数组实际存放的是指向oc对象的指针。

    (2)NSArray的创建

     

     1     //实例方法
     2         NSArray *array = [[NSArray alloc] init];
     3         NSArray *array1 = [NSArray array];
     4         
     5         NSArray *array1 = [[NSArray alloc] initWithObjects:@"123",@"456", nil];
     6         NSArray *array3 = @[@"1234",@"5678"];//见到的时候要认识,但不推荐使用。
     7         NSArray *array2 = [[NSArray alloc] initWithArray:array1];
     8         //打印数组
     9         NSLog(@"array1:%@",array1);
    10         NSLog(@"array2:%@",array2);
    11         NSLog(@"array3:%@",array3);
    12 
    13     //类方法
    14         NSArray *array1 = [NSArray arrayWithObjects:@"456",@"123",@"789", nil];
    15         NSArray *array2 = [NSArray arrayWithArray:array1];

     

    (3)NSArray的访问

     (4)NSArray的遍历

    数组的遍历有以下几种方式:

    首先创建一个数组

     

    第一种方法:使用for循环遍历

     

     

    Block遍历的深入研究:

    每拿到一个元素,就传递给obj,就会调用一次block,并且把当前的元素和索引位置当做参数传递给block。

    注意1:break只使用在两种场合,即switch和循环结构中。

    注意2:stop参数用来停止遍历,其值若为YES,则停止。

     

    (二)NSMutableArray可变数组

     

    1.可变数组创建

     1     //实例方法
     2         NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
     3         NSLog(@"array1 : %@",array1);
     4         
     5         NSMutableArray *array2 = [[NSMutableArray alloc] initWithArray:array1];
     6         NSLog(@"array2 : %@",array2);
     7         
     8         NSMutableArray *array3 = [[NSMutableArray alloc] initWithCapacity:10];
     9         NSInteger lgth = [array3 count];
    10         NSLog(@"%lu",lgth);
    11         
    12         NSMutableArray *array4 = [NSMutableArray array];
    13     类方法
    14         NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"4",@"5",@"6", nil];
    15         NSLog(@"array1:%@",array1);
    16         
    17         NSMutableArray *array2 = [NSMutableArray arrayWithArray:array1];
    18         NSLog(@"array2:%@",array2);
    19         
    20         NSMutableArray *array3 = [NSMutableArray arrayWithCapacity:10];
    21         NSLog(@"array3:%@",array3);

    2.可变数组的使用 

     1     (2)、操作可变数组元素
     2         NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
     3         NSInteger lgth = [array1 count];
     4         NSLog(@"length:%lu",lgth);
     5         NSLog(@"array1:%@",array1);
     6         
     7         [array1 addObject:@"4"];
     8 //        array1 addObjectsFromArray:<#(NSArray *)#>
     9         lgth = [array1 count];
    10         NSLog(@"length:%lu",lgth);
    11         NSLog(@"array1:%@",array1);
    12 
    13         [array1 insertObject:@"4" atIndex:1];
    14         lgth = [array1 count];
    15         NSLog(@"length:%lu",lgth);
    16         NSLog(@"array1:%@",array1);
    17         
    18         [array1 replaceObjectAtIndex:1 withObject:@"10"];
    19         lgth = [array1 count];
    20         NSLog(@"length:%lu",lgth);
    21         NSLog(@"array1:%@",array1);
    22 
    23         [array1 removeObject:@"3"];
    24         [array1 removeObjectAtIndex:0];
    25         array1 removeObject:<#(id)#> inRange:<#(NSRange)#>
    26         [array1 removeLastObject];
    27         [array1 removeAllObjects];
    28         lgth = [array1 count];
    29         NSLog(@"length:%lu",lgth);
    30         NSLog(@"array1:%@",array1);

    (1)NSMutableArray的基本使用

    注意:NSMutableArray继承自NSArray,几乎拥有NSArray的一切方法。

     

     

    二、NSSet和NSMutableSet

    (一)NSSet不可变集合

    基本使用:

     

    (二)NSMutableSet可变集合

    基本使用:

     

    (三)NSSet和NSArray的对比

    (1)共同点:

    1)都是集合,都能够存放多个对象

    2)只能存放oc对象,不能存放非oc对象类型(如int等基本数据类型和结构体,枚举等)。

    3)本身都不可变,都有一个可变的子类。

    (2)不同点:

    1)NSArray有顺序,NSSet没有顺序

    三、NSDictionary和NSMutableDictionary

    (一)NSDictionary不可变字典

    (1)介绍

    现实中的字典:根据索引找到具体的内容

    OC中的NSDictionary:根据key找到value。里面存储的东西都是键值对。

    (2)NSDictionary的创建

     

    注意:快速创建字典是编译器特性。

    (3)NSDictionary的访问

    //根据值找key

    [newDic1 allKeysForObject:@"price"];

     1         //OC字典
     2         //NSDictionary和NSMutableDictionary
     3         //键(key)-值(value) --->KVO设计模式,key-value-O**
     4         
     5         //关于字典,key-value的顺序。字典是无序的,数组是有序的。
     6         //一对键值 是字典的一个元素
     7 //        NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"ET",@"name",@"1800+",@"age",@"Mars",@"address",@"Alians",@"sex", nil];
     8 //        NSLog(@"dic1:%@",dic1);
     9 //        
    10 //        NSDictionary *dic2 = [[NSDictionary alloc] initWithDictionary:dic1];
    11 //        NSLog(@"dic2:%@",dic2);
    12 //        
    13 //        NSArray *objArr = @[@"Albert",@"M",@"China"];
    14 //        NSArray *keyArr = @[@"name",@"sex",@"country"];
    15 //        NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:objArr forKeys:keyArr];
    16 //        NSLog(@"dic3:%@",dic3);
    17         
    18         
    19 //        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"ET",@"name",@"18",@"age",@"ET",@"Type", nil];
    20 //        NSLog(@"dic:%@",dic);
    21         
    22 
    23 //        NSString *valueForName = [dic objectForKey:@"sex"];
    24 //        NSLog(@"%@",valueForName);
    25 //        NSArray *objArr = [dic allValues];
    26 //        NSLog(@"objArr:%@",objArr);
    27         
    28         //ET
    29 //        NSArray *allKeysForObject = [dic allKeysForObject:@"ET"];
    30 //        NSLog(@"keyArr:%@",allKeysForObject);
    31         //
    32 //        NSArray *allKeys = [dic allKeys];
    33 //        NSString *value0 = [dic objectForKey:[allKeys objectAtIndex:0]];
    34 //        NSString *value1 = [dic objectForKey:[allKeys objectAtIndex:1]];
    35 //        NSLog(@"value0:%@,value1:%@",value0,value1);
    36         
    37 //        NSString *tempValue = [dic valueForKey:@"name"];
    38 //        NSLog(@"%@",tempValue);
    39 
    40         
    41         //可变字典
    42 //        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3",@"value4",@"key4", nil];
    43 //        NSLog(@"%@",dic);
    44 //        
    45 //        NSDictionary *tempDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"valueHeHeDa",@"key2", nil];
    46 //        [dic setValuesForKeysWithDictionary:tempDic];
    47 //        NSLog(@"%@",dic);
    48         
    49 //        [dic setObject:@"value5" forKey:@"key1"];
    50 //        NSLog(@"%@",dic);
    51         //
    52 //        [dic setObject:@"value3" forKey:@"key3"];
    53 //        NSLog(@"%@",dic);
    54         //
    55 //        NSDictionary *tempDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value3",@"key3",@"value4",@"key4", nil];
    56 //        [dic setValuesForKeysWithDictionary:tempDic];
    57 //        NSLog(@"%@",dic);
    58         //
    59 //        [dic removeObjectForKey:@"key1"];
    60 //        NSLog(@"%@",dic);
    61         
    62 //        NSArray *keyArray = [NSArray arrayWithObjects:@"key3",@"key4", nil];
    63 //        [dic removeObjectsForKeys:keyArray];
    64 //        NSLog(@"%@",dic);
    65         
    66 //        [dic removeAllObjects];
    67 //        NSLog(@"%@",dic);
    68         
    69 //        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3",@"value4",@"key4", nil];
    70 //        
    71 //        NSArray *dicKeys = [dic allKeys];
    72 //        NSInteger dicLength = [dicKeys count];
    73 //        NSLog(@"keys->length: %lu",dicLength);
    74 //        
    75 //        NSInteger realDicLength = [dic count];
    76 //        NSLog(@"straight: %lu",realDicLength);

     

    (4)NSDictionary的遍历

     

    (二)NSMutableDictionary可变字典

    (1)NSMutableDictionary的基本使用

     

    (2)NSMutableDictionary的使用注意

     

    注意:这种快速创建的方式只适用于不可变字典。

            字典不允许有相同的key,但允许有相同的value(Object)

    三、字典数组(制作通讯录等)

     

     1  1 #import <Foundation/Foundation.h>
     2  2 
     3  3 int main()
     4  4 {
     5  5 
     6  6 // 字典数组
     7  7     NSArray *persons = @[
     8  8     @{@"name" : @"jack", @"qq" : @"432423423", @"books": @[@"5分钟突破iOS编程", @"5分钟突破android编程"]},
     9  9     @{@"name" : @"rose", @"qq" : @"767567"},
    10 10     @{@"name" : @"jim", @"qq" : @"423423"},
    11 11     @{@"name" : @"jake", @"qq" : @"123123213"}
    12 12     ];
    13 13     
    14 14     // 
    15 15     // NSDictionary *jim = persons[2];
    16 16     
    17 17     
    18 18     // 
    19 19     NSString *bookName = persons[0][@"books"][1];
    20 20     NSLog(@"%@", bookName);
    21 21     //NSArray *array = persons[0][@"books"];
    22 22     
    23 23     //NSLog(@"%@", array);
    24 24     
    25 25     // 先取出1位置对应的字典
    26 26     // 再取出字典中qq这个key对应的数据
    27 27     //NSLog(@"%@", persons[1][@"qq"]);
    28 28     
    29 29     // NSLog(@"%@", jim);
    30 30     return 0;
    31 31 }
  • 相关阅读:
    博客园停更...
    Linux-常用命令汇总
    Linux-目录结构
    Mysql-python连接操作数据库
    Mysql-概念及常用命令
    Mysql-Sql查询汇总
    Mysql-Sql增删改查
    Mysql-Navicat破解使用
    Mysql-环境配置及问题解决
    Fiddler-AutoResponder替换资源
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5028758.html
Copyright © 2011-2022 走看看