Swift Programming Language Flashcards
What is Swift and why is it used for iOS development?
Swift is a powerful and intuitive programming language created by Apple for building apps for iOS, Mac, Apple TV, and Apple Watch. It’s designed for safety, speed, and interactive development.
How do you declare a constant in Swift and provide an example?
Constants are declared using the let
keyword and are immutable. Example: let maximumNumberOfLoginAttempts = 10
What is a variable in Swift and how do you declare one? Provide an example.
Variables in Swift are declared using the var
keyword and can be changed after they’re set. Example: var currentLoginAttempt = 0
Explain the basic syntax for a function in Swift and give an example.
A function in Swift is defined using the func
keyword, followed by the function’s name, parentheses with any parameters, and a body enclosed in braces. Example: func greet(person: String) -> String { return "Hello, \(person)!" }
Describe how to implement a conditional if-else statement in Swift with an example.
If-else statements in Swift are used for conditional operations. Example: if score > 60 { print("Passed") } else { print("Failed") }
What is an array in Swift and how do you create one? Provide an example.
An array is a collection of ordered items. In Swift, arrays are declared by placing elements in square brackets, separated by commas. Example: var colors = ["Red", "Blue", "Green"]
Explain the concept of optionals in Swift with an example.
Optionals in Swift are types that can hold either a value or nil
to indicate the absence of a value. Example: var age: Int? = nil
How do you create a simple class in Swift? Include an example.
Classes in Swift are created using the class
keyword followed by properties and methods. Example: class Vehicle { var speed = 0 func description() -> String { return "Traveling at \(speed) miles per hour" } }
What is a closure in Swift and provide a simple example?
Closures are self-contained blocks of functionality that can be passed around and used in your code. Example: let sayHello = { (name: String) -> String in return "Hello, \(name)" }
Explain how to handle errors in Swift using a try-catch block. Provide an example.
Swift uses error handling with do
, try
, and catch
keywords. Example: do { let result = try someFunctionThatCanThrow() print("Result: \(result)") } catch { print("An error occurred: \(error)") }
How do you implement a switch statement in Swift? Provide an example.
A switch statement provides an efficient way to compare many types of data. Example: switch someValue { case 'value1': print('First case') case 'value2': print('Second case') default: print('Default case') }
What is a dictionary in Swift and how do you use it? Provide an example.
A dictionary stores associations between keys of the same type and values of the same type in an unordered collection. Example: var namesOfIntegers = [Int: String]() namesOfIntegers[16] = 'sixteen'
Placeholder Question 1
Placeholder Answer 1
Placeholder Question 2
Placeholder Answer 2
Placeholder Question 3
Placeholder Answer 3
Placeholder Question 4
Placeholder Answer 4
Placeholder Question 5
Placeholder Answer 5
Placeholder Question 6
Placeholder Answer 6
Placeholder Question 7
Placeholder Answer 7
Placeholder Question 8
Placeholder Answer 8
Placeholder Question 9
Placeholder Answer 9
Placeholder Question 10
Placeholder Answer 10
Placeholder Question 11
Placeholder Answer 11
Placeholder Question 12
Placeholder Answer 12