What is the difference between a Struct, a proto, and a class?
What are Classes in Swift?
Classes in Swift are similar to Structures in Swift. These are building blocks of flexible constructs. You can define class properties and methods like constants, variables, and functions are defined. In Swift 4, you don’t need to create interfaces or implementation files while declaring classes.
Benefits of Swift Classes
- By using classes, you can apply inheritance to acquire properties of one class to another class.
- Type casting enables the user to check class type at run time.
- Deinitializers take care of releasing memory resources.
- Reference counting allows the class instance to have more than one reference.
Syntax
What is a struct in Swift?
A struct in Swift is a value type which, just like classes, can contain:
- properties
- methods
- subscripts
- initializers
- protocol conformances
- extensions
It can also be seen as a template definition of an object, like in the following Article instance definition:
syntax-
What are protocols and how do they work in Swift?
Generally, a protocol:
- Is a blueprint that a class or struct follows
- Is a communication contract for unrelated objects to rely on
- Defines methods and values
To understand how protocols work in Swift, let’s suppose we are building application software and must model the requirements to satisfy the application. We can either begin with a superclass and mold the relationship through inheritance or start with a protocol and mold the relationship through the implementation.
If we want to build a salary remittance system for our app and we have an Employee
class, using a protocol looks like the following:
syntax-
Swift: Protocol vs. Struct vs. Class
these analogies are not “exactly” correct, but this is the best way I can tell it
Protocols are effectively like interfaces
Classes are classes, like in Java/Android, and pretty much any other language
Structs are like classes, but they are passed by-value (copied) when passing them from one variable/function to another.If you’re familiar with C# at all, it’s implementation of structs is very similar.
Classes vs Structs
- Both structs and classes can define properties to store values, and they can define functions
- They can define subscripts to provide access to values with subscript syntax
- They can define initializers to set up their initial state, with init()
- They can be extended with extension (this is important!)
- They can conform to protocols, for example, to support Protocol Oriented Programming
- They can work with generics to provide flexible and reusable types
Classes support a few more capabilities that structs don’t have:
- Classes can inherit from another class like you inherit from UIViewController to create your own view controller subclass
- Classes can be deinitialized, i.e. you can invoke a deinit() function before the class is destroyed
- Classes are reference types and structs are value types
protocol vs class
When writing protocol-oriented Swift, protocols and classes become fairly similar, but they are never the same.
In their basic form, a protocol describes what an unknown type of object can do. You might say it has two or three properties of various types, plus methods. But that protocol never includes anything inside the methods or provides actual storage for the properties.
In a more advanced form, you can write extensions on your protocols that provide default implementations of the methods. You still can’t provide storage for properties, however.
In comparison, classes are concrete things. While they might adopt protocols — i.e., say they implement the required properties and methods — they aren’t required to do that. You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.
source-