• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift学习笔记5

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

五、集合

数组

1、泛型 Array<类型>

2、简化[类型]

var myAr:Array<String>

var names:[String]

myArr = Array<String>()

names = Array<String>(count:10,repeatedValue:"heri")

nums = Array<Int>(count:100,repeatedValue:0)

var values = ["2","3","4","o"]

println(names[1])

name[0]="spring"

for var i=0;i <values.count;i++

{
println(values[i])

}

for v in values

{
println(v)

}

添加元素

var languages = ["swift"]

languages.append("Go")

languages +=["Ruby"]

插入元素

languages.insert("R",atIndex:0)

let subRange = languages[1..<4]

languages[2...4] = ["c++","php"]

清空

languages[0..<languages.count] = []

删除

languages.removeAtIndex(2)

languages.removeLast()

languages.removeAll()

字典

1、泛型 

2、简化

var myDict : Dictionary<String,String>

var health:[String,String]

构造器

myDict = Dictionary<String,String>()

health = ["身高":"180","体重":"70","血压":"90/120"]

读取 var height = health["身高"]

var emptyDict :[String:Double]=[:]

health["体重"]= 80

添加新的key - value对

var result = health.updateValue("50",forkey:"腰围")

desc 默认代表 value

var seasons = ["spring":"春暖花开","summer":"夏日炎炎"]

for (season,desc)in seasons

{

println("\(season) -->\(desc)")

}

var keys = Array(seasons.keys)

var values = Array(seasons.values)

删除

var languages = ["swift":900,"php":340,"oc":48]

languages.removeValueForKey("php")

languages["oc"]=nil

languages.removeAll()

实战例子: 记录字符串中每个字符出现的次数,并且对比哪个出现最多

let str = "renhairui is a best man xhahahaxxxxx"

var status :[Character:Int] =[:]

for ch in str

{

if num !=nil

{

var num = status[ch]

status[ch] = num! +1

}else{

status[ch] = 1

}

}

println(status)

var maxOccurs = 0

for occurs in Array(status.values)

{

if occurs >maxOccurs

{

maxOccurs = occure

}

for (ch,occurs)in status

{

if occurs == maxOccurs

{

println("出现最多的字符:\(ch),出现次数:\(occurs)次")

}

}

 

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift3.0的改变发布时间:2022-07-13
下一篇:
如何假装写过 Swift?发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap