The Swift 3 Programming Language – String, Array, Dictionary

Include Values in String

// Declare integer variables
let apples = 3
let oranges = 5

// Include variable values in string using \()
let appleSummary = "I have \(apples) apples and \(oranges) oranges."

// Another example with calculations in \()
let fruitSummary = "I have \(apples+oranges) pieces of fruits."

Experiment - Use \() to include a floating-point calculation in a string to include someone's name in a greeting

let temp = 10.30
let tempSummary = "Current temp is \(temp+temp)"

Create arrays & Dictionaries

// Declaring an array of string
var shoppingList = ["catfish", "water", "tulips", "blue paint"]

// replacing object at index 1 with new value
shoppingList[1] = "bottle of water"

// Declaring a dictionary with type string-to-string
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic"

// adding a pair to dictionary
occupations["Jayne"] = "Public Relations"

Initializing with empty

// Example of creating empty array of type String
let emptyArray = [String]()

// Example of creating empty dictionary string-to-float
let emptyDictionary = [String:Float]()

Click here to open actual swift-playground file for above illustration