final and const (let) keywords | Swift

Tony Trejo
1 min readFeb 16, 2023
final and const (let) keywords | Swift

final

  • Is used to prevent a class, method, or property from being overridden in a subclass.
  • If a class is marked as “final”, no other class can inherit from it and it cannot be changed by subclassing.
  • if a method is marked as “final”, it cannot be overridden in a subclass, ensuring that its implementation remains the same.

final class Button {
final var title: String
final func sayHello() { print("Hello") }
init(title: String) {
self.title = title
}
}

const (let)

  • Constants can be declared using the “let” keyword.
  • Constants declared using “let” must be set to a value when they are declared and cannot be changed afterwards.
  • “const” is not a keyword in Swift.
let maximumItems = 100

Hey there! Are you passionate about mobile development? So am I! That’s why my channel is dedicated to providing you with the latest and greatest in the mobile world. From new technologies to insider tips and tricks, you won’t want to miss a thing. So why not join my community of mobile mavens and let’s stay ahead of the game? Hit that follow button and let’s get started!

--

--