IOS开发之旅-IOS常用数据结构NSArray、NSMutableArray、NSDictionary、NSMutableDictionary介绍

  • NSArray

  • NSArray基本用法
void arrayTest1()
{
    //数组初始化最后必须以nil结尾,表示数组元素结束
    NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil];
    NSLog(@"%@",array1);
    /*(
     item1,
     item2,
     item3,
     item4
     )*/

    //获取数组元素个数
    NSLog(@"array count : %ld",array1.count);
    /*array count : 4*/

    //获取特定所以元素
    NSLog(@"array[1] = %@",array1[1]);
    /* array[1] = item1 */
    NSLog(@"array[1] = %@",[array1 objectAtIndex:(NSInteger)1]);
    /* array[1] = item1 */
    NSLog(@"firstObject = %@",array1.firstObject);  //获取第一个元素
    /* firstObject = item1 */
    NSLog(@"lastObject = %@",array1.lastObject);    //获取最后一个元素
    /* lastObject = item4 */

    //拼接数组
    NSLog(@"%@",[array1 componentsJoinedByString:@","]);
    /* item0,item1,item2,item3,item4 */

    //查找元素的索引,如果包含特定元素,返回具体索引,否则返回:9223372036854775807
    NSLog(@"%ld",[array1 indexOfObject:@"item11"]);

    //是否包含特定元素
    NSLog(@"%i", [array1 containsObject:@"item1"] == TRUE ? 1 : 0);
    /* 1 */

    NSArray *subArray = [array1 subarrayWithRange:NSMakeRange(1, 2)];
    NSLog(@"%@",subArray);
    /*
     (
     item1,
     item2
     )
     */

    //保存数组元素到本地文件,以xml格式序列化存储
    [array1 writeToFile:@"/Users/new/Desktop/array.txt" atomically:YES];
    /*
     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
     <plist version="1.0">
     <array>
     <string>item0</string>
     <string>item1</string>
     <string>item2</string>
     <string>item3</string>
     <string>item4</string>
     </array>
     </plist>
     */

    //读取本地文件,反序列化为数组
    NSArray *array2 = [[NSArray alloc]initWithContentsOfFile:@"/Users/new/Desktop/array.txt"];
    NSLog(@"%@",array2);
    /*
     (
     item0,
     item1,
     item2,
     item3,
     item4
     )
     */

    //判断数组元素是否相等
    BOOL result = [array1 isEqualToArray:@[@"item0",@"item1",@"item2"]];
    NSLog(@"%@",result);
    /* 0 */}
  • NSArray遍历
void arrayTest2()
{
    NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil];

    //遍历方法1
    for (int index = 0; index < array1.count; index++) {
        NSLog(@"%@",array1[index]);
    }
    /*
     item0
     item1
     item2
     item3
     item4
     */

    //遍历方法2
    for (NSString *item in array1) {
        NSLog(@"%@",item);
    }
    /*
     item0
     item1
     item2
     item3
     item4
     */

    //遍历方法3
    //NSEnumerator *enumerator = [array1 reverseObjectEnumerator]; //逆向遍历
    NSEnumerator *enumerator = [array1 objectEnumerator];          //正向遍历
    NSString *item = nil;
    while (item = [enumerator nextObject]) {
        NSLog(@"%@",item);
    }
    /*
     item0
     item1
     item2
     item3
     item4
     */

    //遍历方法4
    [array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%ld = %@",idx,obj);
    }];
    /*
     0 = item0
     1 = item1
     2 = item2
     3 = item3
     4 = item4
     */
}
  • NSArray排序
void arrayTest3()
{
    //排序方法1,普通比较器
    NSArray *array1 = @[@"1",@"21",@"11",@"5",@"51",@"3"];
    NSArray *array2 = [array1 sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",array2);
    /*
     (
     1,
     11,
     21,
     3,
     5,
     51
     )
     */

    //排序方法2,代码块
    NSArray *array3 = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2];
    }];
    NSLog(@"%@",array3);
    /*
     (
     1,
     11,
     21,
     3,
     5,
     51
     )
     */

    //排序方法3,自定义排序描述符
    NSArray *originalArray = @[
                                @{@"page_no":@"27",@"age":@24},
                                @{@"page_no":@"1", @"age":@23},
                                @{@"page_no":@"1", @"age":@21},
                                @{@"page_no":@"1", @"age":@25},
                                @{@"page_no":@"1", @"age":@15},
                                @{@"page_no":@"12",@"age":@19},
                                @{@"page_no":@"23",@"age":@29},
                                @{@"page_no":@"3", @"age":@22},
                                @{@"page_no":@"2", @"age":@30},
                                @{@"page_no":@"17",@"age":@33}
                            ];
    NSSortDescriptor *alphaNumSD = [NSSortDescriptor sortDescriptorWithKey:@"page_no" ascending:YES comparator:^NSComparisonResult(NSString *string1, NSString *string2) {
        return [string1 compare:string2 options:NSNumericSearch];
    }];
    NSSortDescriptor *dataNumSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES comparator:^NSComparisonResult(id data1, id data2) {
        return [data1 compare:data2];
    }];
    NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[alphaNumSD,dataNumSD]];
    NSLog(@"%@",sortedArray);
    /*
     (
     {
     age = 15;
     "page_no" = 1;
     },
     {
     age = 21;
     "page_no" = 1;
     },
     {
     age = 23;
     "page_no" = 1;
     },
     {
     age = 25;
     "page_no" = 1;
     },
     {
     age = 30;
     "page_no" = 2;
     },
     {
     age = 22;
     "page_no" = 3;
     },
     {
     age = 19;
     "page_no" = 12;
     },
     {
     age = 33;
     "page_no" = 17;
     },
     {
     age = 29;
     "page_no" = 23;
     },
     {
     age = 24;
     "page_no" = 27;
     }
     )
     */

    //排序方法4,自定义比较方法
    Person *person1 = [[Person alloc]initWithFirstName:@"hello" lastName:@"world" age:35];
    Person *person2 = [[Person alloc]initWithFirstName:@"abc" lastName:@"def" age:25];
    Person *person3 = [[Person alloc]initWithFirstName:@"cal" lastName:@"kdl" age:22];
    NSArray *personArray = @[person1,person2,person3];
    NSArray *tempArray = [personArray sortedArrayUsingSelector:@selector(comparePeople:)];
    NSLog(@"%@",tempArray);
    /*
     (
     " firstName: abc,lastName:def,age:25",
     " firstName: cal,lastName:kdl,age:22",
     " firstName: hello,lastName:world,age:35"
     )
     */

}
  • NSMutableArray

  NSMutableArray继承NSArray,所以NSArray的所有特性NSMutableArray都有,在此基础上,NSMutableArray主要新增了添加、删除、更新、插入等特性,如下代码演示:

void arrrayTest4()
{
    NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4", nil];
    NSLog(@"%@",mutableArray);
    /*
     (
     item0,
     item1,
     item2,
     item3,
     item4
     )
     */

    //添加元素
    [mutableArray addObject:@"item5"];
    NSLog(@"%@",mutableArray);
    /*
     (
     item0,
     item1,
     item2,
     item3,
     item4,
     item5
     )
     */

    //插入元素
    [mutableArray insertObject:@"inserted item" atIndex:(NSInteger)1];
    NSLog(@"%@",mutableArray);
    /*
     (
     item0,
     "inserted item",
     item1,
     item2,
     item3,
     item4,
     item5
     )
     */

    //删除元素
    [mutableArray removeObject:@"item5"];
    NSLog(@"%@",mutableArray);
    /*
     (
     item0,
     item1,
     item2,
     item3,
     item4
     )
     */

    //更新元素
    [mutableArray replaceObjectAtIndex:(NSInteger)5 withObject:@"replacedItem"];
    NSLog(@"%@",mutableArray);
    /*
     (
     item0,
     "inserted item",
     item1,
     item2,
     item3,
     replacedItem
     )
     */

    //删除所有元素
    [mutableArray removeAllObjects];
    NSLog(@"%ld",mutableArray.count);
    /* 0 */
}
  • NSDictionary

  • 字典初始化
void arrayTest5()
{
    //字典初始化方式1
    NSDictionary *dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value0",@"key0",
                                                                           @"value1",@"key1",
                                                                           @"value2",@"key2",
                                                                           @"value3",@"key3",
                                                                           @"value4",@"key4",
                                                                           nil];

    NSLog(@"%@",dictionary1);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     }
     */

    //字典初始化方式2
    NSArray *valueArray = @[@"value0",@"value1",@"value2",@"value3",@"value4"];
    NSArray *keyArray = @[@"key0",@"key1",@"key2",@"key3",@"key4"];
    NSDictionary *dictionary2 = [[NSDictionary alloc]initWithObjects:valueArray forKeys:keyArray];
    NSLog(@"%@",dictionary2);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     }
     */

    //字典初始化方式3
    NSDictionary *dictionary3 = @{
                                 @"key0":@"value0",
                                 @"key1":@"value1",
                                 @"key2":@"value2",
                                 @"key3":@"value3",
                                 @"key4":@"value4"
                                 };
    NSLog(@"%@",dictionary3);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     }
     */

    //调用NSDictionary静态方法生成字典
    NSDictionary *dictionary4 = [NSDictionary dictionaryWithObject:@"value0" forKey:@"key0"];
    NSLog(@"%@",dictionary4);
    /*
     {
     key0 = value0;
     }
     */

    NSDictionary *dictionary5 = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    NSLog(@"%@",dictionary5);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     }
     */

    NSDictionary *dictionary6 = [NSDictionary dictionaryWithObjectsAndKeys:@"value0",@"key0",@"value1",@"key1",@"value2",@"key2",@"value3",@"key3",@"value4",@"key4", nil];
    NSLog(@"%@",dictionary6);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     }
     */
}
  • 字典基本常用用法
void arrayTest6()
{
    NSDictionary *dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value0",@"key0",
                                 @"value1",@"key1",
                                 @"value2",@"key2",
                                 @"value3",@"key3",
                                 @"value4",@"key4",
                                 @"value4",@"key5",
                                 nil];
    NSLog(@"%@",dictionary1);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     key5 = value4
     }
     */

    NSLog(@"%ld",dictionary1.count);
    /* 5 */

    //输出特定元素
    NSLog(@"%@",dictionary1[@"key0"]);
    NSLog(@"%@",[dictionary1 objectForKey:@"key0"]);
    /* value0 */

    //获取所有key
    NSLog(@"%@",dictionary1.allKeys);
    /*
     (
     key3,
     key1,
     key4,
     key2,
     key0
     )
     */

    //获取所有value
    NSLog(@"%@",dictionary1.allValues);
    /*
     (
     value3,
     value1,
     value4,
     value2,
     value0
     )
     */

    //获取特定value对应的所有key
    NSLog(@"%@",[dictionary1 allKeysForObject:@"value4"]);
    /*
     (
     key4,
     key5
     )
     */

    //分别获取key对应的value,对于不存在的key,返回"not found"
    NSLog(@"%@",[dictionary1 objectsForKeys:@[@"key0",@"key1",@"key8"] notFoundMarker:@"not found"]);
    /*
     (
     value0,
     value1,
     "not found"
     )
     */

    NSDictionary *dictionaryOther = @{@"key0":@"value0"};
    BOOL result = [dictionary1 isEqualToDictionary:dictionaryOther];
    NSLog(@"%hhd",result);
    /* 0 */

    //保存到本地文件
    [dictionary1 writeToFile:@"/Users/new/Desktop/dictionary.txt" atomically:YES];
    /*
     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
     <plist version="1.0">
     <dict>
     <key>key0</key>
     <string>value0</string>
     <key>key1</key>
     <string>value1</string>
     <key>key2</key>
     <string>value2</string>
     <key>key3</key>
     <string>value3</string>
     <key>key4</key>
     <string>value4</string>
     <key>key5</key>
     <string>value4</string>
     </dict>
     </plist>
     */

    //读取本地文件,反序列化为字典
    NSDictionary* dictionary2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/new/Desktop/dictionary.txt"];
    NSLog(@"%@",dictionary2);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     key5 = value4;
     }
     */

    //修改字典元素值,如果key存在,则修改对应的value,如果key不存在,则插入一个新元素
    [dictionary2 setValue:@"update value0" forKey:@"key0"];
    NSLog(@"%@",dictionary2);
    /*
     {
     key0 = "update value0";
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     key5 = value4;
     }
     */

    //插入一个新元素,该key不存在
    [dictionary2 setValue:@"key not found" forKey:@"noKey"];
    NSLog(@"%@",dictionary2);
    /*
     {
     key0 = "update value0";
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     key5 = value4;
     noKey = "key not found";
     }
     */
}
  • NSDictionary遍历
void dictionaryTest()
{
    NSDictionary *dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value0",@"key0",
                                 @"value1",@"key1",
                                 @"value2",@"key2",
                                 @"value3",@"key3",
                                 @"value4",@"key4",
                                 @"value4",@"key5",
                                 nil];
    //遍历方法一
    for (NSString *key in dictionary1) {
        NSLog(@"%@ = %@",key,dictionary1[key]);
    }
    /*
     key3 = value3
     key1 = value1
     key4 = value4
     key2 = value2
     key0 = value0
     key5 = value5
     */

    //遍历方法二
    //NSEnumerator *objectEnumerator = [dictionary1 objectEnumerator];    //objectEnumerator获取value枚举
    NSEnumerator *keyEumerator = [dictionary1 keyEnumerator];           //keyEnumerator获取key枚举
    NSString *key = nil;
    while (key = [keyEumerator nextObject]) {
        NSLog(@"%@ = %@",key,dictionary1[key]);
    }
    /*
     key3 = value3
     key1 = value1
     key4 = value4
     key2 = value2
     key0 = value0
     key5 = value5
     */

    //遍历方法三
    [dictionary1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSLog(@"%@ = %@",key,obj);
    }];
    /*
     key3 = value3
     key1 = value1
     key4 = value4
     key2 = value2
     key0 = value0
     key5 = value5
     */

}
  • NSMutableDictionary

NSMutableDictionary继承于NSDictionary,在此基础上新增加对元素新增、删除、更新等操作,如下代码演示

void mutableDictionaryTest()
{
    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc]initWithCapacity:7];
    NSLog(@"%ld",mutableDictionary.count);
    /* 0 */
    [mutableDictionary setValue:@"value0" forKey:@"key0"];
    [mutableDictionary setValue:@"value1" forKey:@"key1"];
    [mutableDictionary setValue:@"value2" forKey:@"key2"];
    [mutableDictionary setValue:@"value3" forKey:@"key3"];
    [mutableDictionary setValue:@"value4" forKey:@"key4"];
    [mutableDictionary setValue:@"value5" forKey:@"key5"];
    [mutableDictionary setValue:@"value6" forKey:@"key6"];
    [mutableDictionary setValue:@"value7" forKey:@"key7"];
    NSLog(@"%ld",mutableDictionary.count);
    /* 8 */
    NSLog(@"%@",mutableDictionary);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     key5 = value5;
     key6 = value6;
     key7 = value7;
     key8 = value8;
     }
     */

    //删除元素
    [mutableDictionary removeObjectForKey:@"key7"];
    NSLog(@"%ld",mutableDictionary.count);
    /* 7 */
    NSLog(@"%@",mutableDictionary);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     key5 = value5;
     key6 = value6;
     }
     */

    //对于key存在,则修改对应的value,否则添加新元素
    [mutableDictionary setValue:@"update value6" forKey:@"key6"];
    NSLog(@"%@",mutableDictionary);

    //移除多个key对应的元素
    [mutableDictionary removeObjectsForKeys:@[@"key6",@"key5"]];
    NSLog(@"%@",mutableDictionary);
    /*
     {
     key0 = value0;
     key1 = value1;
     key2 = value2;
     key3 = value3;
     key4 = value4;
     }
     */

    //移除所有元素
    [mutableDictionary removeAllObjects];
    NSLog(@"%ld",mutableDictionary.count);
    /* 0 */
}

  以上代码在开发过程中比较常用,如果有不足之处,请给我留言

时间: 2024-12-29 23:07:16

IOS开发之旅-IOS常用数据结构NSArray、NSMutableArray、NSDictionary、NSMutableDictionary介绍的相关文章

关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎么学习它们呢? 我认为学习这些常用的类,使用类比的方法就行了,只要学会一个类,就能类比另外两个类了.. 比如,NSString和NSMutableString的可变与不可变,主要区分的标准是,它们本身是不是可以变,可变的一般都能增,删,改,而不可变的方法主要是创建,查看,得到元素个数或者长度等等;

NSString,NSMutableString, NSArray ,NSMutableArray,NSDictionary,NSMutableDictionary 深拷贝,浅拷贝分析

NSString,NSMutableString, NSArray ,NSMutableArray,NSDictionary,NSMutableDictionary 深拷贝,浅拷贝. 首先我们得知道什么是深拷贝,什么事浅拷贝. 简单点说深拷贝就是拷贝内容,浅拷贝就是拷贝指针. 上面那些类我们可以这样理解他们的深.浅拷贝.只要上面那些类中的NSString,NSArray,NSDictionary这三个类的实例化对象是调用copy方法进行拷贝那么他们拷贝的就是一个指针,就是说他们只是拷贝了一个指向

iOS开发数据库篇—SQLite常用的函数

iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename,   // 数据库的文件路径 sqlite3 **ppDb          // 数据库实例 ); 2.执行任何SQL语句 int sqlite3_exec( sqlite3*,                                  // 一个打开的数据库实例 const char *sql,                    

iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist

iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) 4.SQLite3(数据库,关系型数据库,不能直接存储对象,要编写一些数据库的语句,将对象拆开存储) 5.Core Data(对象型的数据库,把内部环节屏蔽) 二.应用沙盒 每个iOS应用都有?己的应?沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应?必须待在?己的沙盒里,其

文顶顶 iOS开发UI篇—IOS开发中Xcode的一些使用技巧

iOS开发UI篇—IOS开发中Xcode的一些使用技巧 一.快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n     新建项目 cmd + n             新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车           显示主窗口 cmd + 0             导航窗口 option + cmd + 0    工具窗口 在.m & .h之间切换           control + cmd + 上/下 按

文顶顶 iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发UI基础—IOS开发中Xcode的一些使用技巧

iOS开发UI基础-IOS开发中Xcode的一些使用技巧 一.快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n     新建项目 cmd + n             新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车           显示主窗口 cmd + 0             导航窗口 option + cmd + 0    工具窗口 在.m & .h之间切换           control + cmd + 上/下

iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发UI篇—ios应用数据存储方式(归档)

iOS开发UI篇-ios应用数据存储方式(归档)  一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息) 归档:因为前两者都有一个致命的缺陷,只能存储常用的类型.归档可以实现把自定义的对象存放在文件中. 二.代码示例 1.文件结构 2.代码示例 YYViewController.m文件 1 // 2 // YYViewController.m 3 // 0