Skip to content

[swift2objc] Support Computed Properties and Property Observers (i.e get, set, willSet and didSet) #1932

Open
@nikeokoronkwo

Description

@nikeokoronkwo

This issue is to implement support for Computed Properties and Property Observers

Computed Properties contain syntax for getting and setting properties in a compound declaration: get and set.

When it comes to Property Observers, they are called when properties are to be assigned. There are two: willSet and didSet.
From https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties

willSet is called just before the value is stored.
didSet is called immediately after the new value is stored.

// getters and setters in Protocols
protocol Account {
    var balance: Double { get set } // Get and Set
    var accountType: String { get } // Get only

    func displayDetails()
}

// getters and setters in Structs
class CheckingAccount: Account {
    var balance: Double {
        get {
            return internalBalance
        }
        set {
            internalBalance = newValue
            print("Balance updated to \(internalBalance)")
        }
    }

    func displayDetails() {
        print("Account Type: \(accountType), Balance: \(balance)")
    }
}

Getters and setters in structures and classes are usually already defined before-hand, while those in Protocols are not.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions