• Swift字典


    字典初始化

     

    基本的语法:

     

    [key 1: value 1, key 2: value 2, key 3: value

    3]

     var   airports:    Dictionary<String,       String>    = ["TYO": "Tokyo", "DUB":"Dublin"]

     字典追加元素

     var   airports:    Dictionary<String,       String>    = ["TYO": "Tokyo", "DUB":"Dublin"] airports["LHR"] = "London"

     println("The dictionary of airports contains

    (airports.count) items.")

     

    字典删除元素

     

    通过 removeValueForKey(key)方法删除

     var airports: Dictionary<String, String> =

    ["TYO": "Tokyo", "DUB": "Dublin"]

     

    if         let          removedValue                   =

    airports.removeValueForKey("DUB") {

     

    println("The removed airport's nameis (removedValue).")

     

    } else {

     

    println("The airports dictionary doesnot contain a value forDUB.")

     

    }

     

    字典长度

     

    使用 count 属性。

     var   airports:    Dictionary<String,       String>    = ["TYO": "Tokyo", "DUB":"Dublin"]

     println("The dictionary of airports contains

    (airports.count) items.")

    字典遍历

     

    1.遍历方法

     var airports = ["TYO": "Tokyo", "DUB": "Dublin"]

     for (airportCode,airportName) in airports {

     println("(airportCode): (airportName)")

     }

     

    2.遍历键和值

     

    for airportCode in airports.keys {

     println("Airport code: (airportCode)")

     }

     for airportName in airports.values {

     println("Airport name: (airportName)")

     }

     

     

     

    获得键或值的数组:

    let airportCodes= Array(airports.keys)

     // airportCodes is ["TYO", "LHR"]

     

    let airportNames = Array(airports.values)

      // airportNamesis ["Tokyo","London Heathrow"]

     

    Swift交流讨论论坛论坛:http://www.cocoagame.net

    欢迎增加Swift技术交流群:362298485

     



  • 相关阅读:
    kb-07线段树-12--二分查找区间边界
    kb-07线段树--11--区间多重该值多种查询
    kb-07线段树--10--dfs序建树
    kb-09-线段树--区间合并比较繁
    kb-07线段树-08--区间开根
    kb-07-RMQ线段树--07(动态规划)
    kb-07线段树-06离散化(与第四题类似)
    kb-07线段树-05-区间整体修改查询;(水)
    poj-2486-Apple Tree
    mnist的数据预处理
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7074445.html
Copyright © 2020-2023  润新知