ios-swift
/

Swift OOP – Classes, Structs & Protocols

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

ios-swift

Swift OOP – Classes, Structs & Protocols

The Architecture of Swift Objects

Swift is a multi-paradigm language, but its OOP capabilities are its backbone. Unlike Dart, where almost everything is an object (Reference Type), Swift leans heavily on Structs for data modeling. As an Architect, your first decision when creating a new model in the Revochamp iOS runner is choosing between a Struct and a Class.

  1. Classes vs. Structs: The Golden Rule

In Swift, Classes are Reference Types (shared instances), while Structs are Value Types (copied when passed around). Apple recommends using Structs by default and only using Classes when you specifically need inheritance or identity (e.g., a Database Manager or a UI Controller).

FeatureStruct (Value)Class (Reference)
CopyingCopy-on-assignmentShared instance
InheritanceNoYes
SpeedFast (Stack allocation)Slower (Heap allocation)
Thread SafetySafe (Unique copies)Requires care (Shared state)
InitializersMemberwise (Auto-generated)Manual required

  1. Defining a Class

Classes support inheritance, allowing one class to adopt the characteristics of another. This is identical to the 'extends' keyword in Dart.

SWIFTRead-only
1
class Widget {
    var width: Double
    var height: Double

    init(width: Double, height: Double) {
        self.width = width
        self.height = height
    }

    func render() {
        print("Rendering generic widget")
    }
}

class Button: Widget {
    override func render() {
        print("Rendering a button at \(width)x\(height)")
    }
}

  1. Protocols (Interfaces)

In Swift, we don't use 'Abstract Classes' to define contracts. Instead, we use Protocols. A protocol defines a blueprint of methods or properties that a class, struct, or enum must implement. This is the foundation of 'Protocol-Oriented Programming'.

SWIFTRead-only
1
protocol CodeGeneratable {
    var language: String { get }
    func generate() -> String
}

struct FlutterGenerator: CodeGeneratable {
    let language = "Dart"
    func generate() -> String {
        return "void main() { ... }"
    }
}

  1. Encapsulation & Access Control

Swift provides granular control over who can see and modify your code. For an Engineering Manager, enforcing these is key to maintaining a clean API boundary.

  • private: Visible only within the defining declaration.
  • fileprivate: Visible within the entire file.
  • internal (Default): Visible to the entire module (e.g., your iOS app target).
  • public: Visible to other modules, but cannot be subclassed.
  • open: Highest level; visible and can be subclassed by other modules.

Test Your Knowledge

Q1
of 3

Which of these is a 'Value Type' in Swift?

A
Class
B
Protocol
C
Struct
D
Closure
Q2
of 3

Which keyword is used in Swift to allow a subclass to provide a new implementation of a parent's method?

A
new
B
virtual
C
override
D
mutating
Q3
of 3

What is the primary tool for defining 'contracts' and achieving decoupling in Swift architecture?

A
Inheritance
B
Protocols
C
Extensions
D
Singletons

Frequently Asked Questions

What is 'Memberwise Initializer' in Structs?

If you define a Struct, Swift automatically creates a constructor for you based on its properties. For Classes, you MUST write your own 'init()' method manually, which is a common point of friction for developers moving from Dart.

Can a Struct implement a Protocol?

Yes. One of Swift's greatest strengths is that Structs can implement protocols, have methods, and use extensions, making them incredibly powerful compared to basic 'Data Classes' in other languages.

Does Swift have 'mixins' like Dart?

Swift doesn't have a direct 'with' keyword for mixins, but you can achieve identical behavior using 'Protocol Extensions.' You can provide a default implementation for a protocol method, and any type adopting that protocol will automatically get that functionality.

Previous

swift functions

Next

swift classes structs

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.