Swift 语言基础(3)-数组和字典类型

Swift 语言基础(3)-数组和字典类型,第1张

概述Arrays Array Literals var shoppingList: String[] = ["Eggs","Milk"]  // shoppingList has been initialized with two initial items varshoppingList = ["Eggs","Milk"] println("The shopping list contains\(s Arrays Array literals var shopPingList: String[] = ["Eggs","Milk"]
// shopPingList has been initialized with two initial items

varshopPingList = []

println("The shopPing List contains\(shopPingList.count)items.")
// prints "The shopPing List contains 2 items."

ifisEmpty{
"The shopPing List is empty."}else "The shopPing List is not empty."}
// prints "The shopPing List is not empty."


shopPingList . append ( "Flour" )
// shopPingList Now contains 3 items,and someone is making pancakes

+= "Baking Powder"
// shopPingList Now contains 4 items

+= ["Chocolate Spread""Cheese""Butter"// shopPingList Now contains 7 items

firstItem = [0// firstItem is equal to "Eggs"

shopPingList] = "Six eggs"
// the first item in the List is Now equal to "Six eggs" rather than "Eggs"

4...6] = ["Bananas""Apples"// shopPingList Now contains 6 items

insert"Maple Syrup" atIndex: 0)
// shopPingList Now contains 7 items
// "Maple Syrup" is Now the first item in the List


letmapleSyrup removeAtIndex(// the item that was at index 0 has just been removed
// shopPingList Now contains 6 items,and no Maple Syrup
// the mapleSyrup constant is Now equal to the removed "Maple Syrup" string


firstItem// firstItem is Now equal to "Six eggs"

apples removeLast()
// the last item in the array has just been removed
// shopPingList Now contains 5 items,and no cheese
// the apples constant is Now equal to the removed "Apples" string


数组遍历 foritem in shopPingList println(item}
// Six eggs
// Milk
// Flour
// Baking Powder
// Bananas


forindexvalue)enumerate) {
"Item+ 1:"// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas


someInts Int[]()
"someInts is of type Int[] withsomeInts// prints "someInts is of type Int[] with 0 items."

append3// someInts Now contains 1 value of type Int
= []
// someInts is Now an empty array,but is still of type Int[]


threeDoubles Double[](:repeatedValue0.0// threeDoubles is of type Double[],and equals [0.0,0.0,0.0]

anotherThreeDoubles Array2.5// anotherThreeDoubles is inferred as Double[],and equals [2.5,2.5,2.5]

var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as Double[],2.5]

字典 字典字面量 [ key 1 : value 1,key 2 : value 2,key 3 : value 3 ]

airports:Dictionary<String> = ["TYO":"Tokyo""dub""dublin"]

var airports = [ "TYO" : "Tokyo" , "dub" : "dublin" ]

访问与修改字典对象
"The dictionary of airports contains // prints "The dictionary of airports contains 2 items."

["LHR""London"
// the airports dictionary Now contains 3 items

"London Heathrow"
// the value for "LHR" has been changed to "London Heathrow"

if letoldValue airportsupdateValue"dublin International"forKey: "The old value for dub was oldValue."// prints "The old value for dub was dublin."

airportname ] {
"The name of the airport is airportname"That airport is not in the airports dictionary."// prints "The name of the airport is dublin International."


"APL""Apple International"
// "Apple International" is not the real airport for APL,so delete it
] = nil
// APL has Now been removed from the dictionary


removedValue removeValueForKey"The removed airport's name is removedValue"The airports dictionary does not contain a value for dub."// prints "The removed airport's name is dublin International."

字典遍历 airportCodeairports {
// TYO: Tokyo
// LHR: London Heathrow


airportCode keys"Airport code: // Airport code: TYO
// Airport code: LHR


airportname values"Airport name: }
// Airport name: Tokyo
// Airport name: London Heathrow



airportCodes // airportCodes is ["TYO","LHR"]
airportnames // airportnames is ["Tokyo","London Heathrow"]


创建空字典对象 namesOfIntegers DictionaryInt>()
// namesOfIntegers is an empty Dictionary<Int,String>

namesOfIntegers16"sixteen"
// namesOfIntegers Now contains 1 key-value pair
= [:]
// namesOfIntegers is once again an empty dictionary of type Int,String


可变性 For dictionarIEs,immutability also means that you cannot replace the value for
an existing key in the dictionary. An immutable dictionary’s contents cannot be
changed once they are set.
Immutability has a slightly different meaning for arrays,however. You are still
not allowed to perform any action that has the potential to change the size of
an immutable array,but you are allowed to set a new value for an existing
index in the array. This enables Swift’s Array type to provIDe optimal
performance for array operations when the size of an array is fixed
总结

以上是内存溢出为你收集整理的Swift 语言基础(3)-数组和字典类型全部内容,希望文章能够帮你解决Swift 语言基础(3)-数组和字典类型所遇到的程序开发问题。

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

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

原文地址: https://www.outofmemory.cn/web/1088502.html

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

发表评论

登录后才能评论

评论列表(0条)

保存