字典,集合

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"v3", @"k3", @"v5", @"k5", @"v4", @"k4", @"value6", @"key6", @"value7", @"ke7", nil];

NSLog(@"%@", dictionary);

//  使用类方法创建

NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

NSLog(@"%@", dictionary2);

NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

NSLog(@"%@", dictionary3);

NSArray *keyArray = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; //  存放键的数组

NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];//  存放值的数组

NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects: keyArray forKeys:valueArray];

NSLog(@"%@", dictionary4);

// 创建空字典

NSDictionary *dic = [NSDictionary dictionary];

//  使用一个文件创建字典对象 新建文件步骤:command + N -> Resource -> Property List

NSDictionary *dictionary5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5-20140425/lesson5-20140425/dictionary.plist"];

NSLog(@"%@", dictionary5);

//  返回字典中键值对的个数

NSLog(@"%ld", [dictionary5 count]);

//  字典取值 获取指定key对应的value

NSString *value1 = [dictionary5 objectForKey:@"ly"];

NSLog(@"%@", value1);

//  返回所有的key数组

NSArray *allKeysArray = [dictionary5 allKeys];

NSLog(@"%@", allKeysArray);

//  返回所有的value数组

NSArray *allValuesArray = [dictionary5 allValues];

NSLog(@"%@", allValuesArray);

//  使用key枚举器(获取所有key)

NSEnumerator *enumerator = [dictionary5 keyEnumerator];

NSString *str = nil;

while (str = [enumerator nextObject]) {

    NSLog(@"%@", str);

}

4.2创建可变字典 NSMutableDictionary

NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];

//  用于整理对象的拼接

[dic1 addEntriesFromDictionary:dic2];

NSLog(@"%@", dic1);

//  删除字典中某个对象

[dic1 removeObjectForKey:@"k1"];

NSLog(@"%@", dic1);

//  删除字典全部对象

[dic1 removeAllObjects];

NSLog(@"%@", dic1);

//  设置字典

[dic1 setDictionary:dic2];

NSLog(@"%@", dic1);

5.1NSSet集合对象 容器类

//  1. 使用类方法创建对象

NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

NSSet *set2 = [NSSet setWithObject:@"abc"];

NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

NSLog(@"%@", set3);

NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

NSLog(@"%@", set4);

NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

NSLog(@"%@", set5);

//  2.使用实例方法创建

NSSet *set6 = [[NSSet alloc] init];

NSLog(@"%@", set6);

NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

NSLog(@"%@", set7);

NSSet *set8 = [[NSSet alloc] initWithArray:array];

NSLog(@"%@", set8);

NSSet *set9 = [[NSSet alloc] initWithSet:set7];

NSLog(@"%@", set9);

//  3.返回几个元素个数

NSLog(@"%ld", [set7 count]);

//  4.枚举器访问集合元素

NSEnumerator *enumerator = [set7 objectEnumerator];

NSString *str = nil;

while (str = [enumerator nextObject]) {

    NSLog(@"%@", str);

}

//  5.判断两个几个是否有交集

BOOL ifhasIntersection = [set2 intersectsSet:set3];

NSLog(@"%d", ifhasIntersection);

//  6.判断两个集合是否相等

NSLog(@"%d", [set2 isEqualToSet:set3]);

//  7.判断当前集合是否是子集

NSLog(@"%d", [set2 isSubsetOfSet:set3]);

5.2可变集合 NSMutableSet

//  创建指定元素个数的一个集合对象

NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

[mutableSet addObject:@"aaa"];

NSLog(@"%@", mutableSet);

//  类方法创建可变集合

NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

//  添加一个对象到集合

[mutableSet2 addObject:@"ddd"];

NSLog(@"%@", mutableSet2);

//  从集合中删除一个对象

[mutableSet2 removeObject:@"ddd"];

NSLog(@"%@", mutableSet2);

//  把数组对象添加到集合对象中

NSArray *arr = [NSArray arrayWithObjects:@"eee", @"fff", nil];

[mutableSet1 addObjectsFromArray:arr];

NSLog(@"%@", mutableSet1);

//  得到两个集合的交集 注意intersectSet和intersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool值

[mutableSet1 intersectSet:mutableSet2];

NSLog(@"%@", mutableSet1);

//  从一个集合中减去另一个集合

[mutableSet1 minusSet:mutableSet2];

NSLog(@"%@", mutableSet1);

//  从一个元素中删除所有元素

[mutableSet2 removeAllObjects];

NSLog(@"%@", mutableSet2);

//  取两个集合的并集

[mutableSet1 unionSet:mutableSet2];

NSLog(@"%@", mutableSet1);

NSLog(@"%@", mutableSet1);

//  设置给集合赋值

[mutableSet1 setSet:mutableSet2];

NSLog(@"%@", mutableSet1);

关于数组排序

NSArray *array = [NSArrayarrayWithObjects:@"12", @"454", @"7878", @"77122", @"555", @"9", nil];

NSLog(@"排序前:%@", array);

//  这样排序是按照字符串大小来排序的不是数值排序

array = [array sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"排序后:%@", array);

//  进行数值排序需要用以下的方法

array = [array sortedArrayUsingFunction:intSort context:NULL];

NSLog(@"排序后:%@", array);

以下是intSort 的方法实现:

NSInteger intSort(id num1, id num2, void *context)

{

    int v1 = [num1 intValue];

    int v2 = [num2 intValue];

    if (v1 < v2)

    returnNSOrderedAscending;

    else if (v1 > v2)

    returnNSOrderedDescending;

    else

    returnNSOrderedSame;

}

关于字典与数组

/// 字典里可以存数组,数组可以存字典

NSArray *arr = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];

//  把数组arr放在字典dic里

NSDictionary *dic = [NSDictionary dictionaryWithObject:arr forKey:@"array"];

NSLog(@"%@", dic);

//  把字典dic和数组arr 放在数组arr2里

NSArray *arr2 = [NSArray arrayWithObjects:arr, dic, nil];

NSLog(@"%@", arr2);

字典,集合

时间: 2024-08-08 13:54:41

字典,集合的相关文章

python :列表 字典 集合 类 ----局部变量可以改全局变量

#列表 字典 集合 类 ----局部变量可以改全局变量,除了整数和字符串 names=["alex","jack","luck"] def func(names): names[0]='金角大王' print("inside name:" ,names) func(names) print (names) #字符串 name='jack' name1=name name='jack_chen' print(name,name1

python中列表 元组 字典 集合的区别

列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复项的.如果我们扩大清单的范围,统计我们过去一周所有的花费情况,那么这也是一个清单,但这个清单里会有类别不同的项,比如我们买东西是一种花费,交水电费也是一种花费,这些项的类型是可以使不同的.pyt

【学习ios之路:Object-C】字典.集合.

1.不可变数据字典(NSDicionary) 字典:用来存储具有一一对应关系的数据. 一个key 对应一个 value ,key起到了唯一标示的作用,key必须是唯一的,但是一个vlaue可以对应多个key. 字典存储的是无序的元素,一对键值对组成了字典中的一个元素. ①.不可变字典初始化赋值 <span style="font-size:18px;"> //笑笑语法 NSDictionary *dic = @{@"name":@"zhangd

Swift字典集合

字典表示一种非常复杂的集合,允许按照某个键来访问元素.字典是由两部分集合构成的,一个是键(key)集合,一个是值(value)集合.键集合是不能有重复元素的,而值集合是可以重复的,键和值是成对出现的. 如下图所示是字典结构的"学号与学生"集合,学号是键集合,不能重复,学生是值集合,可以重复. 提示 字典中键和值的集合是无序的,即便在添加的时候是按照顺序添加的,当取出这些键或值的时候,也会变得无序.字典集合更适合通过键快速访问值,就像查英文字典一样,键就是要查的英文单词,而值是英文单词的

OC 字典 集合

用字典能有什么好处? 字典是个大容器,它能够储存多个数据 用字典存储的数据具有一一对应的关系(使用key来标识value) 字典中一对键值对(key-value)叫做字典中的一个元素,也叫一个条目,只要是对象就可以,不限制类型 字典是无序的 字典中的key是唯一的,一个key只能对应一个value,一个value可以对应多个key 创建字典对象: 1.便利构造器(+号方法): NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKe

c#将枚举转换成字典集合

枚举在软件开发中的用途 1. 枚举类型(enum type)是具有一组命名常量的独特的值类型. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } 或者:如果只给男赋值,那么女=1 public enum Sex { 男 = 0, 女 } 3. 我们在实际开发中,对于数据库的设计会经常需要很多状态字段(比如性别.审核状态.分类状态等等等等),而这些状态字段的值又只有固定的几个,这个时候我们一般会需要数据字典来维护这些数据.而数据字典该以什么形式存在呢? 以我自己

python基础一 -------如何在列表字典集合中根据条件筛选数据

如何在列表字典集合中根据条件筛选数据 一:列表 先随机生成一个列表,过滤掉负数 1,普通for循环迭代判断 2,filter()函数判断,filter(函数,list|tuple|string) 1 filter(lambda x:x>0,data) 3,列表推倒式 4,效率对比:还是列表推导式稍高 二:字典 1,跟列表类似,推导式 先生成随机的字典(key从1-20) 过滤掉value是负数的值 三:集合 随机生成10个元素的集合 过滤掉小于0的元素,跟字典类似

初识Swift集合之字典集合

字典集合 字典表示一种非常复杂的集合, 允许按照某个键来访问元素 字典集合的声明与初始化: var strudentDictionary1 : Dictionary<Int , String> = [102 : " Jack" , 105 : "Mark" , 107 : "Jay"] ; //这里声明里一个strudentDictionary1 的字典集合,他的键是 Int 类型,他的值为String类型 var strudentD

.Net学习笔记----2015-06-25(File类的读写文件、List泛型集合、装箱和拆箱、Dictionary字典集合)

File类:静态类,Create Delete Copy Move ,主要用来对数据对文本文件进行读写 File类:缺点:只能读写小文件 读写操作: //byte[] buffer = File.ReadAllBytes(@"C:\Users\Administrator\Desktop\new.txt"); ////将字节数组中的每一个元素都要按照我们指定的编码各式解码成字符串 ////UTF-8 GB2312 GBK ASCII Unicode //string s = Encodi

WPF TreeView绑定字典集合

1 <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width="292" > 2 3 <TreeView.ItemTemplate> 4 <HierarchicalDataTemplate ItemsSource="{Binding Value}"> 5 <StackPanel> 6