微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐