Five, set
Array
1. Generic type array< >
2. Simplification [Type]
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)
}
adding elements
var languages = ["Swift"]
Languages.append ("Go")
Languages +=["Ruby"]
inserting elements
Languages.insert ("R", atindex:0)
Let SubRange = languages[1..<4]
LANGUAGES[2...4] = ["C + +", "PHP"]
Empty
Languages[0..<languages.count] = []
Delete
Languages.removeatindex (2)
Languages.removelast ()
Languages.removeall ()
Dictionary
1. Generic type
2. Simplifying
var mydict:dictionary<string,string>
var health:[string,string]
Constructors
Mydict = dictionary<string,string> ()
Health = ["height": "180", "Weight": "70", "blood pressure": "90/120"]
Read var height = health["height"]
var emptydict: [string:double]=[:]
health["Weight"]= 80
Add a new Key-value pair
var result = Health.updatevalue ("", Forkey: "Waist circumference")
Desc Default represents value
var seasons = ["Spring": "Spring Flowers", "Summer": "Summer scorching"]
for (SEASON,DESC) in seasons
{
println ("\ (season)-->\ (DESC)")
}
var keys = Array (Seasons.keys)
var values = Array (seasons.values)
Delete
var = ["Swift": Languages, "PHP": 340, "OC": 48]
Languages.removevalueforkey ("PHP")
languages["OC"]=nil
Languages.removeall ()
Practical example: Record the number of occurrences of each character in a string, and compare which one appears most
Let str = "Renhairui are 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 ("Occurrences of the most characters: \ (ch), occurrences: \ (occurs) times")
}
}
}
Swift Learning Note 5