Pages

Tuesday, February 10, 2015

Creating Array and dictionary in Swift : Swift Tutorial








Creating array using array is similar in swift and objective c.

Ex:
var numbers = ["one", "two", "three", "four"]
numbers[1] = "ten"

In this example the value of numbers first position value is changed to "ten"
var dicVal = [
"bruce Lee": "Fighter",
"Jordan": "Mechanic",
]
dicVal["Jordan"] = "Automobile Engineer"

In this example the value of dicVal "Jordan" value is changed to "Automobile Engineer""

To create an empty array or dictionary, use the initializer syntax.
let emptyArray = [String]()
let emptyDictionary = [String: Float]()

If type information can be inferred, you can write an empty array as [] and an empty dictionary as [:]—for example, when you set a new value for a variable or pass an argument to a function.
shoppingList = []
occupations = [:]


var shoppingList : [String] = ["Unknown", "Arab"]
println("The shopping list contains \(shoppingList.count) items.")
if shoppingList.isEmpty {
    println("The shopping list is empty.")
} else {
    println("The shopping list is not empty.")
}
shoppingList.append("Spanish")
shoppingList += ["French"]
var firstItem = shoppingList[0]
shoppingList[0] = "English"
shoppingList[0...3] = ["Chines", "Hindi"]
shoppingList.insert("Italian", atIndex: 0)
let mapleSyrup = shoppingList.removeAtIndex(0)

No comments:

Post a Comment