Swift for Beginners Part 2: Variables & Constants

Swift for Beginners Part 2: Variables & Constants

Introduction to Swift

In this section, the instructor introduces Swift as a programming language created by Apple in 2014 to modernize and replace Objective-C. The instructor also mentions that Swift is mainly used for creating iOS apps, Mac OS apps, and Apple Watch apps.

Variables, Constants, and Statics

  • A variable is a thing that you assign to another thing. It can point to two types of things: primitives (e.g., strings and numbers) or objects (e.g., classes that model things in the real world).
  • A constant is similar to a variable but cannot be changed once it has been assigned.
  • A static is similar to a constant but can be created multiple times if multiple copies of the file or object exist.

Examples

  • An example of using variables: var x = 4 and var y = 1. We can add them together with x + y.
  • An example of using constants: let z = 12. Attempting to change it later will result in an error.
  • An example of using statics: creating an instance of a class called "person" with var john = person().

Understanding Static Variables in Swift

In this section, we will learn about static variables in Swift and how they differ from constants and variables.

Definition of a Static Variable

  • A static variable is similar to a constant but the difference is that if we create multiple objects, every single one will have the same value for the static variable.
  • We can access a static variable without creating an instance of it.

Practical Example of Static Variables

  • In iOS apps, there is an object called the transaction queue that handles all transactions and payments.
  • The transaction queue is a statically created object because we only want one thing processing transactions.
  • This ensures that only one payment confirmation window appears at a time, preventing confusion for users.

Differences Between Constants and Variables

  • A variable can be changed while a constant cannot be changed once it has been assigned.
  • Each instance of an object has its own copy of a variable or constant unless it is declared as static.

Constants vs Variables in Swift

In this section, we will learn about the differences between constants and variables in Swift.

Definition of Constants and Variables

  • A variable points to something else and its assignment can vary.
  • The name given to a variable is also called an instance.
  • A constant is the exact same thing as a variable but its value cannot be reassigned once set.

Examples of Constants and Variables

  • If we create multiple instances of an object with different names, each instance has its own copy of any non-static variables within it.
  • If we create multiple instances with different values for a static variable, each instance will have the same value for that static variable.

Understanding Variables, Constants, and Statics in Swift

In this video, the instructor explains the concepts of variables, constants, and statics in Swift. He provides examples to help viewers understand how these concepts work.

Variables and Constants

  • Variables are used to store values that can be changed later on in the program.
  • Constants are used to store values that cannot be changed once they have been set.
  • Both variables and constants need a type annotation when declared.

Statics

  • Statics are used to create properties or methods that belong to a class rather than an instance of a class.
  • A perfect candidate for something that we want to make static is when we only want one thing processing transactions like payments.
  • A static needs to go inside of a class so it needs to be part of an object.
  • You cannot put a static outside of a class like we did for variables and constants; it needs to be inside a class which is synonymous with an object.

Conclusion

  • Variables, constants, and statics are fundamental building blocks in Swift programming.
  • It's important to understand these concepts as they will be used throughout your programming journey in Swift.

The video ends with music playing at 15 minutes 1 second but there is no additional content after the conclusion section above

Video description

iOS Academy offers the #1 course for iOS Development for Beginners. Learn to create iPhone and iPad apps from scratch. Everyone these days has an app idea; learn the important skill of making those apps, make yourself more marketable, change industries to get a different job, and enjoy the ride!