Swift字典

Swift字典,第1张

概述//// main.swift// Swift字典//// Created by Goddog on 15/6/25.// Copyright (c) 2015年 Goddog. All rights reserved.//import Foundation//声明和创建字典//使用泛型语句声明字典var myDict : Dictionary<String,Str
////  main.swift//  Swift字典////  Created by Goddog on 15/6/25.//  copyright (c) 2015年 Goddog. All rights reserved.//import Foundation//声明和创建字典//使用泛型语句声明字典var myDict : Dictionary<String,String>//用简化语句声明字典var scores : [String : Int]var health : [String : String]//使用构造器创建字典//创建一个Dictionary结构体,使用默认的参数myDict = Dictionary<String,String>()scores = Dictionary<String,Int>(minimumCapacity: 5)//使用简化语法创建字典health = ["身高":"178","体重":"65","形象":"帅气"]println(health)var emptyDict : [String:Double] = [:]println(emptyDict.isEmpty)//使用字典var height = health["身高"]println(height)var noExist = health["没有哦"]println(noExist)//指定key对应的valuehealth["形象"] = "极佳"println(health)//对不存在的key设置valuescores["JAVA"] = 90scores["PHP"] = 80scores["Object-c"] = 99println(scores)//由于字典根据key访问value返回的是包含value的可选类型,对字典返回的value使用感叹号进行强制解析var myscore : Int? = scores["计算机"]if myscore != nil{    println("计算机成绩是:\(myscore!)")}var result = scores.updateValue(20,forKey: "JAVA")println(result)println(scores)//使用for - in 遍历字典var seasons = ["spring":"春天花会开","summer":"夏天夏天悄悄","autumn":"秋天不回来","winter":"冬天里一把火"]for (season,desc) in seasons{    println("\(season)--->\(desc)")}//单独使用字典的key和values//将所有seasons的所以key和values包装成各自的Array集合var keys = Array(seasons.keys)var values = Array(seasons.values)println("key集合是:\(keys)")println("values集合是:\(values)")//字典的可变性和字典的修改var languages = ["Swift":100,"AndroID":98,"PHP":97,"Go":79,"Perl":68,"Ruby":87,"OC":88]//添加、修改key-value对languages.updateValue(80,forKey: "Hbase")//删除key - value对languages.removeValueForKey("OC")   //删除println(languages)
[体重: 65,形象: 帅气,身高: 178]trueOptional("178")nil[体重: 65,形象: 极佳,身高: 178][Object-c: 99,PHP: 80,JAVA: 90]Optional(90)[Object-c: 99,JAVA: 20]summer--->夏天夏天悄悄spring--->春天花会开winter--->冬天里一把火autumn--->秋天不回来key集合是:[summer,spring,winter,autumn]values集合是:[夏天夏天悄悄,春天花会开,冬天里一把火,秋天不回来][PHP: 97,Go: 79,Perl: 68,Hbase: 80,Swift: 100,AndroID: 98,Ruby: 87]Program ended with exit code: 0
总结

以上是内存溢出为你收集整理的Swift字典全部内容,希望文章能够帮你解决Swift字典所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1091490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存