1、字典写法

Dictionary<KeyType,ValueType>,KeyType是你想要储存的键,ValueType是你想要储存的值。

唯一的限制就是KeyType必须是可哈希的,就是提供一个形式让它们自身是独立识别的

Swift的全部基础类型都能够

2、创建字典

var airport :Dictionary<String, String> = ["TYO": "Tokyo", "DUB": “Dublin"]
var namesOfIntegers = Dictionary<Int, String>()
namesOfIntegers[16] = "sixteen"

3、字典元素个数

airports.count

4、字典加入�一个元素

airports["LHR"] = "London"

5、使用下标语法去改变一个特定键所关联的值。

airports["LHR"] = "London Heathrow"
updateValue(forKey:) 方法返回一个和字典的值同样类型的可选值.
比如,假设字典的值的类型时String,则会返回String? 或者叫“可选String“,这个可选值包括一个假设值发生更新的旧值和假设值不存在的nil值。       
if let oldValue = airports.updateValue("Dublin International", forKey: "DUB") {
println("The old value for DUB was \(oldValue).")
}

6、获取key所相应的值

let airportName = airports["DUB"]

使用下标语法把他的值分配为nil,来移除这个键值对。

7、移除key相应的值

airports["APL"] = "Apple International"
// "Apple International" 不是 APL的真实机场,所以删除它
airports["APL"] = nil
从一个字典中移除一个键值对能够使用removeValueForKey方法,这种方法假设存在键所相应的值,则移除一个键值对,并返回被移除的值,否则返回nil。
let removedValue = airports.removeValueForKey("DUB")

8、用for in遍历字典

for (airportCode, airportName) in airports {
println("\(airportCode): \(airportName)")
}

读取字典的keys属性或者values属性来遍历这个字典的键或值的集合。

for airportCode in airports.keys {
println("Airport code: \(airportCode)")
}
// Airport code: TYO
// Airport code: LHR
for airportName in airports.values {
println("Airport name: \(airportName)")
}

使用keys或者values属性来初始化一个数组

let airportCodes = Array(airports.keys)
let airportNames = Array(airports.values)

Swift-Dictionary的更多相关文章

  1. swift Dictionary 字典

    // //  main.swift //  字典 // //  Created by zhangbiao on 14-6-15. //  Copyright (c) 2014年 理想. All rig ...

  2. iOS - Swift Dictionary 字典

    前言 public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConverti ...

  3. 寒哥带你深入了解下Swift中的Value Type

    http://www.cocoachina.com/swift/20150923/13539.html 关于开发到底使用ValueType 值类型还是Reference Type 引用类型,关于这个, ...

  4. Swift5 语言指南(十八) 可选链接

    可选链接是一个查询和调用当前可选的可选项的属性,方法和下标的过程nil.如果optional包含值,则属性,方法或下标调用成功; 如果是可选的nil,则返回属性,方法或下标调用nil.多个查询可以链接 ...

  5. Swift 03.Dictionary

    字典 key它必须是可哈希的,也就是说,它必须能够提供一个方式让自己被唯一表示出来.Swift的所有基础类型(例如String.Int.Double和Bool)默认都是可哈希的,这些类型都能够用作字典 ...

  6. [Swift通天遁地]五、高级扩展-(6)对基本类型:Int、String、Array、Dictionary、Date的扩展

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Swift 字典 Dictionary基本用法

    import UIKit /* 字典的介绍 1.字典允许按照某个键访问元素 2.字典是由两部分组成, 一个键(key)集合, 一个是值(value)集合 3.键集合是不能有重复的元素, 值集合可以有重 ...

  8. [Swift]LeetCode269. 外星人词典 $ Alien Dictionary

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  9. [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  10. [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

随机推荐

  1. Win异常: 除了chrome浏览器外,所有安装的软件都连不上网

    经查找资料,是LSP被篡改,恢复后使用正常. 百度百科  LSP: Layered Service Provider, 即分层服务提程序,Winsock 作为应用程序的 Windows 的网络套接字工 ...

  2. visualSVN server库迁移(转)

    转自:http://blog.csdn.net/yuhuijun_1/article/details/9762683 首先,VisualSVN Server Manager,包含两个路径,一个是安装路 ...

  3. 基于jQuery 的图片瀑布流实现

    解题思路: 第1步  分析问题:我这边的处理方式是以列为单位.每次滚动条滚到底部,把需要加的新的内容放在高度最小的列.如下图所示 加载后的显示 如果在继续往下滚动.新图片就会在1下边显示,如此类推. ...

  4. phpcms v9 get调用标签整理

    常见的phpcms标签调用  1.调用本系统单条数据(调用ID为1的信息,标题长度不超过25个汉字,显示更新日期): "select * from phpcms_content where  ...

  5. Python自动化运维之27、Django(一)

    一.概述 1.什么是框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单说就是使用别人搭好的舞台,你来做表演. 2.常 ...

  6. 如何将BarTender内容锁定不让改动

    条码标签代表的是产品的特性等,具有相当的精确性,所以需要保证它的正确性.而使用BarTender软件,可以帮助小伙伴将设计的条码标签内容锁定,保护它而不被人改动.下面,小编就教教大家如何实现BarTe ...

  7. Xamarin Mono错误: unable to find explicit activity class

    unable to find explicit activity class在android开发很常见,网上一般是java的解决办法,对我们这些xamariner就无语了. xamarin中用attr ...

  8. SharePoint REST api

    http://msdn.microsoft.com/en-us/magazine/dn198245.aspx Understanding and Using the SharePoint 2013 R ...

  9. java dump

    注意,请不要被我误导,我没有看其他资料,这是我自己分析的,有些可能是不对的 "DestroyJavaVM" prio=6 tid=0x00316800 nid=0x448 wait ...

  10. java敏感词过滤

    敏感词过滤在网站开发必不可少.一般用DFA,这种比较好的算法实现的. 参考链接:http://cmsblogs.com/?p=1031 一个比较好的代码实现: import java.io.IOExc ...