can we pass parameter in computed property in swift?

swift computed property
swift class variable
swift readonly property
swift static property
swiftui computed property
swift property wrapper
let' declarations cannot be computed properties
stored property and computed property in swift
class a{
    var i:Int{
        return 10
    }
}

in this code, I want to pass parameter in "i" variable and they return this parameter. is this possible in swift?

No, you can't.

To pass one or multiple parameters you have to use a function / method.

However you can assign a closure to a variable.

Swift computed properties - Cosmin Mircea, What is a computed or calculated property in a class in Swift? Swift: Beware of Computed Properties. Yesterday, I accidentally stumbled into yet another cool Swift feature: Computed Properties. I saw it used by someone else in their code, and found the syntax confusing enough at the time to think it did something different than it does! A computed property is essentially a function disguised as a property.

As @vadian already said, you can't. But if you want to do some calculation when setting i you could adjust the setter:

class a {
    private var _i: Int = 0

    var i: Int {
        set {
            _i = newValue + 5 // or whatever you want
        }

        get {
            return _i
        }
    }
}

All about Properties in swift - Abhimuralidharan, variable. These variables are created using a function you specify only when that variable is first requested. Computed Properties as Property Observers. In Swift 4 to observe and respond to property values Property Observers are used. Each and every time when property values are set property observers are called. Except lazy stored properties we can add property observers to 'inherited' property by method 'overriding'.

So, based on Swift 4 you declare a gettable variable and you don't define it as a settable variable, so you cant assign value to var i.

you can use this syntax to assign variable from outside the class:

class a {
    var i: Int = 10 // define var as settable and gettable properties
}

Properties, Stored and computed properties are usually associated with instances of a You can also set and modify the initial value for a stored property during If you pass a property that has observers to a function as an in-out parameter, the willSet  If a protocol requires a property to be gettable and settable, that property requirement can’t be fulfilled by a constant stored property or a read-only computed property. If the protocol only requires a property to be gettable, the requirement can be satisfied by any kind of property, and it’s valid for the property to be also settable if this is useful for your own code.

What is a computed property?, Here's what I'm trying to do : I want to show or hide an element (using v-show ) depending on wether or not the result of a function is true. I've tri. In the previous article, we explored the basics of functions in Swift.Functions, however, have a lot more to offer. In this article, we continue our exploration of functions and look into function parameters, nesting, and types.

What are lazy variables?, To me, this means that I don't write funcs that take no arguments and return a if I later decide to change the computed property into a stored property, I can do  Pass arguments to computed properties Posted 3 years ago by Demers94 Here's what I'm trying to do : I want to show or hide an element (using v-show ) depending on wether or not the result of a function is true.

Pass arguments to computed properties, In this Swift tutorial, we're going to take a look at Swift computed properties and how We can then set the goal property of the goalTracker variable. If we wanted to see percentageCompleted if we pass in 10.0 for our units  One kilometer is the same as 1,000 meters, so the km computed property multiplies the value by 1_000.00 to convert into a number expressed in meters. Similarly, there are 3.28084 feet in a meter, and so the ft computed property divides the underlying Double value by 3.28084 , to convert it from feet to meters.

Comments
  • Do you mean like this? let xy = { (x: Int, y:Int) -> Int! in return x * y }
  • I don't understand, can you give an example code what you want to achieve?
  • What are you trying to achieve?
  • Could you provide an example code? I'm stuck with var kTabs: CGFloat = ((Int?) -> CGFloat) { myInt in return 0.0 }