In this video, Scott explains the similarities and differences between reference type classes and value type structures in the Swift 3 standard library.
- [Instructor] Classes and structures in Swift are close counterparts, so I'm going to compare and contrast them first before getting into working with each one of them later in this chapter. Classes and structures are used to model instances that can store values and perform actions. Classes and structures share many features and capabilities. They can both define properties to store values and methods to perform actions. Both can use subscripting to access their properties. Classes and structures both have initializers to set up their initial state. And they're extendable, meaning you can write extensions to add functionality.
Lastly, both classes and structures can adopt protocols which allows you to define a blueprint or contract declaring properties and methods that a conforming class or structure must implement. Swift is largely a protocol oriented language. Classes and structures also have important distinguishing characteristics that will often come into play when you're deciding whether to choose a class or a structure. Classes are reference types, so when passing around a class instance in code, you are passing a reference to the same instance, whereas structures are value types, which are copied when assigned or passed as an argument to an input parameter.
Only class instances can use a deinitializer, such as to perform some cleanup actions before being deallocated. Subclassing is a common pattern used with classes to inherit properties and methods of a superclass. There is no concept of a super structure and so structures cannot inherit. With classes and structures being so close to feature parody, you might be wondering when to use either a class or a structure. When choosing between a structure or a class, choose a structure if you mostly or only need to store simple value types and do not need to inherit properties or methods and the pass by copies semantics of structures don't get in the way or cause confusion.
For storing complex data types, or when inheritance are having multiple references to the same instance are needed or anticipated, choose a class.
Learn how to write code, understand Swift's key concepts and best practices, and strengthen your programming problem-solving skills. Instructor Scott Gardner teaches the fundamentals, so you'll be prepared to develop applications for iOS, macOS, and other platforms. Completing this course will enable you to not only write first-class code, but to think like a Swift developer.
- Creating playgrounds
- Defining variables and constants
- Working with characters and strings
- Working with collections and groups
- Using operators and defining custom operators
- Controlling program flow
- Defining functions and closures
- Working with classes, structures, and enumerations
- Adopting protocols
Skill Level Beginner
1. Get Started
2. Variables and Constants
3. Characters and Strings
4. Collections and Tuples
6. Control Flow
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.