Join John Nastos for an in-depth discussion in this video When to use what type, part of Learning Swift 2.
- Let's go over some quick definitions of classes, structs, enums, and the basic difference between the three. All three are constructs that allow your program to store, organize, or manipulate data in different ways with different capabilities. First, classes. Apple's documentation calls classes general purpose, flexible constructs that become the building blocks of your program's code. They have properties and methods that provide data storage and functionality to the constructs. Classes can also use inheritance to create a sort of family tree of properties and functions between objects.
They can also have initializers and deinitializers to set up and tear down when they're created or destroyed. Structs in Swift are very similar to classes. Both can use properties to store values and use methods to provide functionality. Both can also have initializers to set up their initial state. Structs, though, have some limitations. The primary limitation is that they have no inheritance capability. Also, while classes are passed by reference, structs are passed by copy.
This can be an advantage or disadvantage, depending on your needs. Lastly, there are enumerations. While classes and structs are very similar, enums are something different. They're a way to group related values. However, unlike enums in C, which are a relatively simple construct, enums in Swift have some more capabilities, including the ability to include functions. Each type has distinct advantages and disadvantages. Sometime this may mean that depending on your goal, there may be only one way to accomplish it.
For example, if you wish to have two different types of objects, let's say apples and oranges, and you want them both to inherit from the parent object called Fruit, you would have to use classes since structs don't have the ability to use inheritance. Other times, there may be multiple ways to achieve the same goals. Maybe a situation where either a class or a struct can get the job done. But your particular programming style may lead you to choose one over the other. Say for example that you need a construct to represent a user for a website.
It may be possible to use a class to represent that user or a struct may work as well. As you learn more about the functionality of classes, structs, and enums and specifically their particular characteristics in Swift if you're coming from another language, you'll get more of a sense of how to make the best use of these three constructs.
First, learn how to install Xcode, the IDE designed for Apple developers, and use Swift playgrounds, an innovative coding environment that displays results as you code. Author John Nastos then dives deep into Swift syntax: the variables and constants, data types, arrays, loops, classes, and functions that make your apps run. Next, discover how to build an engaging app interface with storyboards and Interface Builder, connect the interface and code, and use table views to present lists. John shows how to store and retrieve data, and interact with Objective-C, the original app development language that still integrates beautifully with Swift. Using the exercise files provided with the course, you'll assemble a complete working app with Swift while gaining the skills to go create the next one on your own.
- Downloading and installing Xcode
- Using Swift playgrounds
- Understanding Swift syntax
- Debugging Swift code
- Controlling flow with expressions, conditional statements, and loops
- Using and writing Swift functions
- Creating classes, structs, and enums
- Using storyboards in Interface Builder
- Building an app with table views
- Storing and retrieving data
- Interacting with Objective-C