Difference between Struct, Class and Protocol in Swift

Class

class classname {
Definition 1
Definition 2
..........
Definition n
}

Struct

Why we need structures?

  • To encapsulate simple data values.
  • To copy the encapsulated data and its associated properties by ‘values’ rather than by ‘references’.
struct Person {
var clothes: String
var shoes: String
}

Protocol

protocol Animal {

// blueprint of a property
var name: String { get }


// blueprint of a method
func eat()
}
// conform class to Animal protocol
class Dog: Animal {

// implementation of property
var name = "Tommy"

// implementation of method
func eat() {
print("Eating!")
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store