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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.