Swift Learning Note 5

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.