geri222
11/20/2018 - 3:13 PM

Swift Arrays

Swift arrays stores values of the same data type in an ordered list.

//: ## Arrays
//: ### Instantiating an Array
var friedChickenRecipe:[String]
friedChickenRecipe = ["Mix spices with flour, sugar and salt.",
                      "Dip chicken in egg white, mix in flour mixture.",
                      "Deep fry chicken.",
                      "Drain on paper towels."]
//: ### Add elements to an array
friedChickenRecipe.insert("Check chicken temp is 165°F.", at: 3)
friedChickenRecipe.append("Serve!")
//: ### Looping through an Array
for step in friedChickenRecipe {
  print(step)
}
for (index, step) in friedChickenRecipe.enumerated() {
  print("Step \(index + 1):\(step)")
}
//: ### Extract element from array
let firstStep = friedChickenRecipe.first
let secondStep = friedChickenRecipe[1]
let firstTwoSteps = friedChickenRecipe[0...1]    //Extract (or set) elements using range
//: ### Concatenating Arrays
let preRecipeSteps = ["Preheat oven to 350°F"]
friedChickenRecipe = preRecipeSteps + friedChickenRecipe      //Use + or +=