标签:
//NSDictionary, 不可变字典
//1.如果不创建,不能够使用
//2.内部以key-value(键值对)形势存储
//3.key必须唯一
//4.是无序集合
//5.value必须为对象, key一般为NSString
//name:张三, age:18, gender:男
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"张三", @"name", [NSNumber numberWithInt:18], @"age", @"男", @"gender", nil];
NSLog(@"%@", dic);
//通过key获取value值
NSString *name = [dic objectForKey:@"name"];
NSLog(@"%@", name);
//获取所有的value值
NSArray *values = [dic allValues];
NSLog(@"%@", values);
//获取所有的key值
NSArray *keys = [dic allKeys];
NSLog(@"%@", keys);
//获取键值对的个数
NSUInteger count = [dic count];
NSLog(@"%ld", count);
//NSMutableDictionary,可变字典类, 继承于NSDictionary
//创建可变字典
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithCapacity:0];
//添加键值对
[mDic setObject:@"张三" forKey:@"name"];
[mDic setObject:[NSNumber numberWithInteger:18] forKey:@"age"];
[mDic setObject:@"男" forKey:@"gender"];
//删除键值对
[mDic removeObjectForKey:@"name"];
//修改键值对
[mDic setObject:@"女" forKey:@"gender"];
[mDic setObject:[NSNumber numberWithInteger:15] forKey:@"age"];
NSLog(@"%@", mDic);
//遍历字典的key值, 并打印可以对应的value
//for循环实现
// NSArray *mKeys = [mDic allKeys];
// for (int i = 0; i < [mDic count]; i++) {
// NSLog(@"%@", [mKeys objectAtIndex:i]);
// NSLog(@"%@", [mDic objectForKey:[mKeys objectAtIndex:i]]);
// }
//for in
//1.保证数组中的元素类型必须一致
//2.快速枚举时, 不能对数组进行修改(增删元素)
//for in用于数组, 遍历每一个元素
for (id aKeys in [mDic allKeys]) {
NSLog(@"%@", aKeys);
NSLog(@"%@", [mDic objectForKey:aKeys]);
}
//for in用于字典, 遍历每一个key值
for (id something in mDic) {
NSLog(@"%@", something);
}
//遍历字典value值
for (id avalue in [mDic allValues]) {
NSLog(@"%@", avalue);
}
//数组和字典的嵌套使用
NSMutableDictionary *student1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"王明阳", @"name", [NSNumber numberWithInt:23], @"age", @"男", @"gender", nil];
NSMutableDictionary *student2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"王浩", @"name", [NSNumber numberWithInt:20], @"age", @"男", @"gender", nil];
NSMutableDictionary *student3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"魏子健", @"name", [NSNumber numberWithInt:28], @"age", @"女", @"gender", nil];
NSMutableArray *studentArray = [[NSMutableArray alloc] initWithCapacity:0];
[studentArray addObject:student1];
[studentArray addObject:student2];
[studentArray addObject:student3];
NSLog(@"%@", studentArray);
//遍历数组打印学生信息
for (NSDictionary *astudent in studentArray) {
NSLog(@"name:%@", [astudent objectForKey:@"name"]);
NSLog(@"age:%@", [astudent objectForKey:@"age"]);
NSLog(@"gender:%@", [astudent objectForKey:@"gender"]);
}
//
for (NSMutableDictionary *stu in studentArray) {
for (NSString *key in stu) {
NSLog(@"%@", [stu objectForKey:key]);
}
}
NSMutableArray *nameArray = [[NSMutableArray alloc] initWithObjects:@"张三", @"李四", @"王五", nil];
NSMutableDictionary *girl = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"狗妮儿", @"name", [NSNumber numberWithInteger:18], @"age", @"女", @"gender", nameArray, @"candidate", nil];
NSLog(@"%@", girl);
//打印候选人名单
for (NSString *name in [girl objectForKey:@"candidate"]) {
NSLog(@"%@", name);
}
//NSSet NSDictionary NSArray 都是集合类
//NSSet 不可变集合类
//1.元素必须是对象
//2.无序
//3.元素互异(不能相同)
NSSet *set = [[NSSet alloc] initWithObjects:@"1哈哈", @"2嘿嘿", @"3呼呼", @"4啦啦", @"5啊啊", @"6呀呀", nil];
NSLog(@"%@", set);
//获取元素个数
NSLog(@"%lu", [set count]);
//获取某个元素
NSLog(@"%@", [set anyObject]);
//获取所有元素
NSLog(@"%@", [set allObjects]);
//判断集合中是否包含某个对象
if ([set containsObject:@"2嘿嘿"]) {
NSLog(@"包含");
}
//子集合
NSSet *set1 = [[NSSet alloc] initWithObjects:@"1", @"3", @"4", @"5", nil];
NSSet *set2 = [[NSSet alloc] initWithObjects:@"3", @"4", @"5", nil];
if ([set2 isSubsetOfSet:set1]) {
NSLog(@"set2是set1的子集合");
}
//NSMutableSet, 可变集合类, 继承于NSSet
NSMutableSet *mSet = [[NSMutableSet alloc] initWithCapacity:0];
//添加元素
[mSet addObject:@"1"];
[mSet addObject:@"1"];
[mSet addObject:@"1"];
[mSet addObject:@"2"];
NSLog(@"%@", mSet);
//删除元素
[mSet removeObject:@"1"];
NSLog(@"%@", mSet);
//删除所有元素
[mSet removeAllObjects];
NSLog(@"%@", mSet);
//语法糖
//1.创建字符串
NSString *string = @"iPhone";
//2.创建数组(不可变)
NSArray *array2 = @[@"1", @"2", @"3"];
//创建字典
NSDictionary *dic2 = @{@"name": @"张三", @"age": @12, @"gender": @"男"};
//创建数值类
NSNumber *number = @12;
//数组排序
NSMutableArray *array3 = [[NSMutableArray alloc] initWithObjects:@1, @2, @30, @4, @10, nil];
//冒泡排序
BOOL flag = YES;
for (int i = 0; i < [array3 count] - 1 && flag; i++) {
flag = NO;
for (int j = 0; j < [array3 count] - 1 - i; j++) {
NSNumber *a = [array3 objectAtIndex:j];
NSNumber *b = [array3 objectAtIndex:j + 1];
if (a > b) {
[array3 exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
flag = YES;
}
}
}
NSLog(@"%@", array3);
//这种方式排序, 是用于对人名的排序(字母)
[array3 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", array3);
2-6 LessonNsDictionaryAndNSSet
标签:
原文地址:http://www.cnblogs.com/xiaoxuetongxie/p/4319293.html