UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project 1 and Day 19 Challenge - Properties vs. Methods

Forums > 100 Days of Swift

Can anyone with more experince address why Paul uses calculated properties rather than methods for Project 1?

In working the Day 19 Challenge, I thought it made more sense to use methods because they are performing an action i.e. converting one measurement to another. To me, I would think that would make the code more resuable and easier to read.

For example:

@State private var inputNumber = 1.0

func convertToFeet( _ : Double) -> Double { does the work}

func convertFromFeet( _ : Double) -> Double {does the work}

then, for the result

Text(convertFromFeet(convertToFeet(inputNumber)), format: .number)

Is there a reason to use calculated properties instead that I am just not seeing yet?

3      

Here are a couple of reasons:

  1. Calculated properties are accessed using the syntax of a regular property, like object.property, whereas methods are invoked using function-like syntax, like object.method(). If the computation represents a property of an object that you want to access frequently, a calculated property provides a more natural and concise syntax.

  2. Calculated properties are not stored values but rather perform computations based on other properties or variables. If the value you need to compute is derived from the existing state of the object or depends on other properties, a calculated property is a suitable choice. On the other hand, methods are typically used when you want to perform an action or operation that doesn't necessarily relate directly to the state of the object.

Hope that helps,

Jim

4      

Hi @jmiller1975

They are called Computed properties and are Lazy (eg only calculted when used). Mainly used when the other propeties are in same struct/class. PS you use a computed property for every View (var body: some View)

You will find that you will use both but sometimes it comes down to readablity at call.

struct ContentView: View {
    @State private var amount = 0.0

    var convertedAmountInFeet: Double {
        amount * 1.1 // do stuff
    }

    var body: some View {
        VStack {
            Text(convertedAmountInFeet, format: .number)

            Text(convertToFeet(for: amount), format: .number)
        }
        .padding()
    }

    func convertToFeet(for amount: Double) -> Double {
        amount * 1.1 // do stuff
    }
}

PS I some times end up changing them about in project if I feel that the other is a better fit.

Remeber it is your code.

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.