Difference between a struck, a proto, and a class in Swift

A.M.V.N.Attanayaka
3 min readMay 23, 2022

--

Hello friends, today i am going to discuss about a struck, a proto, and a class in swift, so lets going to talk about that,

What is a struck in swift

In Swift, a struct is used to store variables of different data types. For example,Suppose we want to store the name and age of a person. We can create two variables: name and age and store value.However, suppose we want to store the same information of multiple people.In this case, creating variables for an individual person might be a tedious task. To overcome this we can create a struct that stores name and age. Now, this struct can be used for every person.

What is a proto in swift

A protocol is a set of one or more method declarations and that set has a name and represents a protocol. I say declarations, because the methods that together are defined by a particular protocol, do not have any implementation code defined.. The only thing that exist is their names declared. in class, you have always defined not only what methods the class has, but also how that work will be done. But methods in protocol do not have any implementation.

What is a class in swift

A class serves as a blueprint for creating one or more objects based on specific implementation of that class. A good analogy is a form for cutting out butter-cookies. The form‘s attributes (shape, size, height) define the cookies that you can cut out with it. You have only one form (class) but you can create many cookies (instances of that class, ie. objects) with it. All cookies are based on that particular form. Similarily all objects that are instances of that class are identical in their attributes.

lets talk about different.

Structures and classes in Swift have many things in common. Both can:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind

Classes have additional capabilities that structures don’t have:

  • Inheritance enables one class to inherit the characteristics of another.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
  • Reference counting allows more than one reference to a class instance.

also protocols in a very similar way to classes and structures.

  • Reference counting allows more than one reference to a class instance.Custom types state that they adopt a particular protocol by placing the protocol’s name after the type’s name, separated by a colon, as part of their definition. Multiple protocols can be listed, and are separated by commas
  • Reference counting allows more than one reference to a class instance.
  • The protocol doesn’t specify whether the property should be a stored property or a computed property

So guys, that the end of our artical, hope to connect with interasting artical in next time also

--

--