This document provides a summary of key concepts in Swift including variables and constants, strings, control flow, functions, classes and structures. It defines variables and constants, demonstrates string interpolation and formatting, and covers control structures like for-in loops and switch statements. It also introduces optionals, collections like arrays and dictionaries, and shows the difference between classes and structures in Swift.
This document provides a summary of key concepts in Swift including variables and constants, strings, control flow, functions, classes and structures. It defines variables and constants, demonstrates string interpolation and formatting, and covers control structures like for-in loops and switch statements. It also introduces optionals, collections like arrays and dictionaries, and shows the difference between classes and structures in Swift.
This document provides a summary of key concepts in Swift including variables and constants, strings, control flow, functions, classes and structures. It defines variables and constants, demonstrates string interpolation and formatting, and covers control structures like for-in loops and switch statements. It also introduces optionals, collections like arrays and dictionaries, and shows the difference between classes and structures in Swift.
Variables and Constants Strings and Characters Control Flow
var myImplicitInteger = 7 var concatenate = "Wi" + "Fi" // WiFi var count = 1 var myImplicitDouble = 7.0 var empty = "Wi".isEmpty // false while count < 4 { // while loop var myImplicitBoolean = true var interpolate = "Age:\(4)" // Age:4 count++ var myExplicitString:String = "hi" var initial:Character = "A" // A } let myIntegerConstant = 7 var unicodeHeart = "\u{2665}" // ♥ let level = 7 // switch statement Comments for character in "Hello".characters { switch level { print(character) case 1...4: print("Beginner") // single line comment } default: print("Expert") /* multiple line } comment */ var amount = "4 kgs" amount.insert("2",atIndex:0) // 24 kgs Functions and Closures Tuples amount.removeAtIndex(1) // 2 kgs func greetFriend(name:String) { var month = (7, "July") print("Hi, \(name)!") var monthNumber:Int = month.0 let country = "New Zealand" } var monthName:String = month.1 country.hasPrefix("New") // true greetFriend("Maria") // Hi, Maria! country.hasSuffix("land") // true greetFriend("John") // Hi, John! Optionals Collections var favoriteAnimal:String? = "penguin" Classes and Structures favoriteAnimal = nil var myArray:[Int] = [4, 5, 6] class Point { var x = 1, y = 3 Operators myArray.count // 3 func sum() -> Int { myArray.isEmpty // false var x = 10, y = 3 return x + y var addition = x + y // 13 } var mySet:Set = [9, 7, 8] var subtraction = x - y // 7 } var multiplication = x * y // 30 var pointA = Point() // New instance mySet.insert(5) // 9, 5, 7, 8 var division = x / y // 3 var pointB = pointA // Reference mySet.sort() // 5, 7, 8, 9 var remainder = x % y // 1 pointB.x = 17 // 17 var greaterThan = x > y // true pointA.x // 17 var myDictionary = [1:"A", 2:"B"] var lessThan = x < y // false pointA.sum() // 20 var equal = x == y // false myDictionary.keys // 1, 2 var notEqual = x != y // true struct Shape { myDictionary.values // A, B var not = !true // false var width = 0, height = 0 var and = true && false // false } Control Flow var or = true || false // true var shapeA = Shape(width:2, height:3) for value in myArray { // for-in loop var shapeB = shapeA var ternary = x < y ? "L" : "G" // G total += value shapeB.width = 27 // 27 var nilCoalesce = nil ?? "N" // N } shapeA.width // 2 (value type)