Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nikeokoronkwo opened this issue Jan 23, 2025 · 0 comments

Comments

@nikeokoronkwo
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant