字典

作者: 西门丨不吹雪 | 来源:发表于2016-09-05 11:51 被阅读22次

字典定义: 键值对

OC:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"];
NSLog(@"%@", dict);

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];
NSLog(@"%@", dict);

NSDictionary *dict = @{@"name":@"lnj", @"age":@30};
NSLog(@"%@", dict);

// key一定要是可以hash的(String, Int, Float, Double, Bool), value没有要求
var dict = ["name":"lnj", "age":30]
print(dict)

var dict1:Dictionary = ["name":"lnj", "age":30]
print(dict1)

var dict2:Dictionary<String,Any> = ["name":"lnj", "age":30]
print(dict2)

var dict3:[String:Any] = ["name":"lnj", "age":30]
print(dict3)

var dict4:[String:Any] = Dictionary(dictionaryLiteral: ("name", "lnj"), ("age", 30))
print(dict4)

//不可变数组:
let dict5 = [:]
//可变数组:
var dict6  = [:]

字典操作

OC:
1.获取
NSDictionary *dict = @{@"name":@"lnj", @"age":@30};
NSLog(@"%@", dict[@"name"]);

var dict7 = ["name":"lnj", "age":30]
print(dict7["name"]!)

2.修改
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];
dict[@"name"] = @"iversion";
NSLog(@"%@", dict[@"name"]);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];
[dict setObject:@"iversion" forKey:@"name"];
NSLog(@"%@", dict[@"name"]);

var dict8 = ["name":"lnj", "age":30]
dict8["name"] = "iverson"
print(dict8["name"]!)

var dict9 = ["name":"lnj", "age":30]
dict9.updateValue("iverson", forKey: "name")
print(dict9["name"]!)

 3.更新
// updateValue返回一个可选类型, 如果字典中不存在需要更新的key, 那么返回nil, 如果存在返回原始值
var dict10 = ["name":"lnj", "age":30]
if let orignal = dict10.updateValue("iverson", forKey: "name")
{
print(dict10["name"]!)
print(orignal)
}

// updateValue返回一个可选类型, 如果字典中不存在需要更新的key, 那么返回nil并且会将新的键值对添加到字典中
var dict11 = ["name":"lnj", "age":30]
if let orignal = dict11.updateValue("iverson", forKey: "abc")
{
print(dict11["abc"]!)
print(orignal)
}
print(dict11)

4.添加
OC:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];
dict[@"height"] = @175;
NSLog(@"%@", dict);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name", @"lnj", @"age", @30, nil];
[dict setObject:@175 forKey:@"height"];
NSLog(@"%@", dict);

var dict12 = ["name":"lnj", "age":30]
dict12["height"] = 175;
print(dict12)

5.删除
OC:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];
[dict removeObjectForKey:@"name"];
NSLog(@"%@", dict);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];
[dict removeAllObjects];
NSLog(@"%@", dict);

var dict13 = ["name":"lnj", "age":30]
dict13.removeValueForKey("name")
print(dict13)


// removeValueForKey返回一个可选类型, 如果字典中不存在需要删除的key, 那么返回nil并且不会执行任何操作, 如果存在则删除key对应的值, 并且返回被删除的值
var dict14 = ["name":"lnj", "age":30]
if let orignal = dict14.removeValueForKey("names")
{
print(dict14)
print(orignal)
}
print(dict14)

var dict15 = ["name":"lnj", "age":30]
dict15.removeAll(keepCapacity: true)

遍历字典
OC:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"key = %@ value = %@", key, obj);
}];

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];
NSArray *keys = [dict allKeys];
for (NSString *key in keys) {
NSLog(@"%@", key);
}

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @30, @"age", nil];
NSArray *values = [dict allValues];
for (NSString *value in values) {
NSLog(@"%@", value);
}

var dict16 = ["name":"lnj", "age":30]
for (key , value) in dict16
{
print("key = \(key) value = \(value)")
}

var dict17 = ["name":"lnj", "age":30]
for key in dict17.keys
{
print("key = \(key)")
}

var dict18 = ["name":"lnj", "age":30]
for value in dict18.values
{
print("value = \(value)")
}

相关文章

  • day9-课程总结

    1.字典 增:字典[key] = 值; 字典.setdefaule(key, 值);字典.update(字典)删:...

  • swift--字典

    创建字典 字典的基本操作 遍历字典 字典合并

  • Swift学习系列 字典的使用

    字典的概念 字典的初始化 字典元素的基本操作 字典的基本操作 字典的遍历 字典合并

  • 字典

    创建字典 访问字典中的值 修改、添加字典 修改字典中的值 在末尾增添新的键/值 删除字典元素 删除字典 清空字典 ...

  • 新2019计划:python学习-字典【4】

    字典 本篇章讲述数据结构字典,主要围绕如何访问字典,如何修改字典,如何删除字典某元素,如何遍历字典,字典的常见方法...

  • Swift 基础笔记 - 字典

    字典 定义同样使用 [] 定义字典let 不可变字典var 可变字典 定义空字典 字典常用操作赋值直接使用dict...

  • day8-函数基础

    2.字典 2.1操作字典 2.1.1. clear 字典.clear() 清空字典 2.1.2. copy 字典2...

  • Swift字典

    字典的定义 字典的增删改查 字典的遍历 字典的合并

  • day8-总结

    1.字典相关方法 字典.clear() - 清空字典(删除字典中所有的键值对) 2.copy 字典.copy()-...

  • 字典

    本节大纲 字典的定义与特性 字典的常用操作 字典的遍历 字典的定义与特性 字典的常用操作 字典的遍历-案例 扩展-...

网友评论

      本文标题:字典

      本文链接:https://www.haomeiwen.com/subject/vfsrettx.html