![]() |
Programming Fundamentals in Swift | Coursera Meta |
This course is perfect for beginners eager to grasp the foundational concepts that support the Swift programming language. You'll delve into essential programming ideas and data structures common to all languages, while also exploring the distinctive features that make Swift versatile today.
Throughout the course, you'll engage in practical exercises to apply these concepts. You will learn to work with constants and variables of various data types, and understand how to organize and manage information using collection types like arrays, tuples, and dictionaries. Additionally, you'll discover how to make your code more reusable and expressive through the use of functions and closures.
Designed for beginners aiming for a career in iOS development, this course requires no previous web development experience. All you need are basic internet navigation skills and a willingness to start coding.
Notice!
Always refer to the module on your course for the most accurate and up-to-date information.
Attention!
If you have any questions that are not covered in this post, please feel free to leave them in the comments section below. Thank you for your engagement.
Introduction to programming in Swift
- A variable
- A constant
- The number of words in the English language.
- The number of letters in the English alphabet.
- The number of pages in the English dictionary.
- A variable
- A constant
- ==
- !=
- True
- False
- >
- <
- <=
- >=
- True
- False
- Yes
- No
- ?
- !
let text = "text"if let number = Int(text) {print("The number is \(number).")} else {print("Invalid number!")}
- Invalid number!
- The number is 4.
Data structures
- a for in loop
- a while loop
- a repeat while loop
- a while loop
- a for in loop
- a repeat while loop
- True
- False
- numbers += [5]
- numbers.append(5)
- numbers[5] = 3
- True
- False
- travelMiles.keys
- travelMiles.values
- travelMiles.deleteValue(forKey:"George")
- travelMiles["George"] = nil
var weeklyTemperatures = ["Monday": 70, "Tuesday": 75, "Wednesday": 80, "Thursday": 85, "Friday": 90]weeklyTemperatures["Sunday"]! += 10
- False
- True
let emptyArray: [Int] = [0]if emptyArray.count == 0 {print("The array is empty!")} else {print("The array isn’t empty!")}
- The array isn’t empty!
- The array is empty!
- The code will produce a compile error.
let dailyTemperature = ("Monday", 70)
- Indices.
- Labels
Functions and Closures
- True
- False
- func function-name (argument list) -> return type
- func function-name: return type <- (argument list)
- functionname (argument list) : return type
- { (argument list) -> return type in code to be executed}
- [ (argument list) -> return type in code to be executed]
- ( (argument list) -> return type in code to be executed)
- func hiThere(firstNamefn:String, secondNamesn:String)
- hiThere(fn:"John", sn:"Smith")
- hiThere(firstNamefn:"John", secondNamesn:"Smith")
- hiThere("John", "Smith")
- True
- False
- True
- False
- True
- False
- True
- False
- True
- False
- True
- False
Structures and Classes
- Each instance keeps a unique copy of its data
- Instances share a single copy of the data
struct Employee {var salary: Doublevar tax = 0.2mutating func deductTax() {salary = salary - (tax * salary)}}var emp = Employee(salary: 100)emp.deductTax()print(emp.salary)
- 20
- 80.0
- 100
- 0.2
struct Tax {var amount: Int = 5}var tax1 = Tax()var tax2 = tax1tax1.amount = 20print("\(tax1.amount), \(tax2.amount)")
- 5, 5
- 20, 5
- 20, 20
- 5, 20
class Product {var price: Int = 5}var product1 = Product()var product2 = product1product1.price = 20print("\(product1.price), \(product2.price)")
- 5, 5
- 20, 20
- 5, 20
- 20, 5
class Vehicle {
var type: String
var noOfWheels: Int
init(type: String, wheels: Int) {
self.type = type
noOfWheels = wheels + 1
}
}
let car = Vehicle(type: "Jeep", wheels: 3)
print(car.type, "has", car.noOfWheels, "wheels")
- Jeep has 4 wheels
- Jeep has 3 wheels
- Jeep has 5 wheels
class Square {var width: Double = 0var area: Double {return width * width}}let square = Square()square.width = 2print(square.area)
- 4.0
- 0.0
- 2.0
- Structures come with an auto-generated memberwise initializer, while with classes you must create your own.
- Classes are known as reference types while structures are known as value types.
- Structures can use inheritance and by that extend the subclass.
- Because in Swift standard library you will find that reference types are primary used.
- Because a structure is a value type, and a class is a reference type.
- Because a local change in the code when using a structure will not affect other parts of the code.
- A computed property is read-only
- A stored property is primary used when there is a need to encapsulate a block of computational code.
- A computed property does not store a value.
- When a class property is assigned a default value.
- When there are more than 4 parameters in the class initializer.
- When name of the property in the class is the same as the name of the parameters in the initializer.
Final Graded Quiz
- True
- False
- True
- False
- Double
- Integer
- True
- False
- True
- False
- strings+ ["last item"]
- strings.append("last item")
- strings+= ["last item"]
- True
- False
- True
- False
- True
- False
- ["A":true, "B":false, "C":true, "D":false]
- {"A":true, "B":false, "C":true, "D":false}
- ("A":true, "B":false, "C":true, "D":false)