Swift Tutorials: Understanding `open` and `public` 🎯

beginner
17 min

Swift Tutorials: Understanding open and public 🎯

Welcome to another exciting tutorial! Today, we're going to dive deep into the world of Swift, exploring two essential access control keywords: open and public. These keywords help us manage how our code interacts within our projects, making our code more modular, maintainable, and secure.

Let's get started! 📝

Access Control in Swift 💡

Before we dive into open and public, let's briefly understand the concept of access control in Swift. Access control allows us to restrict or grant access to our code based on its context. In Swift, we have four levels of access control:

  1. Private (denoted by private): Accessible only within the defining declaration.
  2. FilePrivate (denoted by fileprivate): Accessible within the defining file.
  3. public: Accessible everywhere, including other Swift files and modules.
  4. open: Accessible everywhere, including other Swift files, modules, and extensions.

The public Access Level 💡

public is the most commonly used access level in Swift. It allows our code to be accessed by any code within our project or any other project that imports our code.

Defining public Properties and Functions 📝

To declare a public property, simply place the public keyword before the property's type. Here's an example:

swift
public var greeting: String = "Hello, World!"

To define a public function, use the public keyword before the function's declaration. Here's a simple example:

swift
public func greet(name: String) -> String { return "Hello, \(name)!" }

Using public within a Class 📝

If you want a class's methods or properties to be accessible outside the class, declare them as public. Here's an example:

swift
public class Person { public var name: String public var age: Int public init(name: String, age: Int) { self.name = name self.age = age } public func greet() -> String { return "Hi, I'm \(name). Nice to meet you!" } }

The open Access Level 💡

While public allows our code to be accessible within our project and other projects that import it, open makes our code accessible even in extensions. This means that other developers can extend our classes or protocols without violating access control rules.

Defining open Properties and Functions 📝

To declare an open property, simply place the open keyword before the property's type. Here's an example:

swift
open var favoriteFood: String = "Pizza"

To define an open function, use the open keyword before the function's declaration. Here's a simple example:

swift
open func eatFood() { print("Yum! I love \(favoriteFood)!") }

Using open within a Class 📝

If you want a class's methods or properties to be accessible outside the class and extendable, declare them as open. Here's an example:

swift
open class Animal { open var name: String open var legs: Int open init(name: String, legs: Int) { self.name = name self.legs = legs } open func makeSound() -> String { fatalError("This method should be overridden in a subclass.") } }

Now, let's see how we can extend the Animal class:

swift
class Dog: Animal { override var name: String = "Fido" override var legs: Int = 4 override func makeSound() -> String { return "Woof woof!" } }

Quiz Time! 🎯

Quick Quiz
Question 1 of 1

What is the difference between `public` and `open` in Swift?

That's all for today! With a solid understanding of public and open access control in Swift, you're now equipped to write more modular, maintainable, and secure code. Stay tuned for more exciting Swift tutorials! 🚀