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

前端之家收集整理的这篇文章主要介绍了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
原文链接:https://www.f2er.com/swift/327423.html

猜你在找的Swift相关文章