码迷,mamicode.com
首页 > 编程语言 > 详细

Swift # 字典

时间:2015-05-11 23:28:49      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

/*
字典: 存储一组无序数据
格式:
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);

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

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

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

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

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

//空字典:
var dict01:Dictionary<String,Any> = [:]
var dict02 = Dictionary<String,Any>()
var dict03 = [String:Any]()

//不可变数组:var dict:Dictionary<String,Any> = [:]
//可变数组:let dict:Dictionary<String,Any> = [:]


/*
字典操作
OC:
1.获取
NSDictionary *dict = @{@"name":@"lnj", @"age":@30};
NSLog(@"%@", dict[@"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"]);


Swift:*/
//1.获取
var dict04 = ["name":"lnj", "age":30]
println(dict04["name"]!)

//2.修改
var dict05 = ["name":"lnj", "age":30]
dict05["name"] = "iverson"
println(dict05["name"]!)


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


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


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

/*
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);

Swift:*/
var dict09 = ["name":"lnj", "age":30]
dict09["height"] = 175;
println(dict09)


/*
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);


Swift:*/
var dict_1 = ["name":"lnj", "age":30]
dict_1.removeValueForKey("name")
println(dict_1)


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


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

println("====--遍历字典--====");
/*
遍历字典
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);
}

Swift:*/
var dict_a = ["name":"lnj", "age":30]
for (key , value) in enumerate(dict_a)
{
println("key = \(key) value = \(value)")
}

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

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

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

 

|--> Copyright (c) 2015 Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

Swift # 字典

标签:

原文地址:http://www.cnblogs.com/SpongeBob-GitHub/p/4495670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!