Swift 4.2 Setter Getter, All Paths Through This Function Will Call Itself

Swift 4.2 Setter Getter, All paths through this function will call itself

Your issue is that there is no stored property type for the getter to return. type is a computed property. When you try to read its value, it calls the getter you defined. This getter calls the getter, which in turn calls the getter which calls the getter... and so on. You have infinite recursion.

Most likely, what you meant to do is have a stored property, that just has some fancy behaviour whenever its set. Rather than using a computed property with a custom get and set, use a stored property with a willSet or didSet block:

@objc var type: DecisionType {
didSet {
let isDecisionDouble = newValue == DecisionType.DecisionDouble

okButton.isHidden = isDecisionDouble;
yesButton.isHidden = !isDecisionDouble;
noButton.isHidden = !isDecisionDouble;
}
}

Swift: Extension, All paths through this function will call itself

One possible solution is to revert the process:

var isLowAlpha: Bool {
get {
return !isUserInteractionEnabled
}
set {
isUserInteractionEnabled = !newValue
alpha = newValue ? 0.3 : 1
}
}

or better, since you are not interested in the getter, make it a function:

func setIsLowAlpha(_ lowAlpha: Bool) {
self.isUserInteractionEnabled = !lowAlpha
self.lowAlpha = newValue ? 0.3 : 1
}

Anyway, looking at your code, you probably want to implement that a view is disabled. That's usually a task for UIControl subclasses, not UIView.

Also note the same can be implemented using a wrapper view:

class AlphaView: UIView {
var isLowAlpha: Bool = false {
didSet {
isUserInteractionEnabled = !isLowAlpha
alpha = isLowAlpha ? 0.3 : 1
}
}
}

And put your views inside.

Xcode 10.1 swift 4.2 operator overloading causing compiler warning: All paths through this function will call itself

When you do left += right, it calls the same function that you were defining. In other words, your operator overload function += ( left: inout CGVector, right: CGVector) will call itself in all times (infinite recursion). You are doing something like

func foo(String: bar) {
foo(bar)
}

But just by replacing foo with +=, which is not logical. Xcode only gives you a warning now though, it is not an error that stops you from compiling. You probably have written this function wrong in the past (but the warning reminding you this was just added to the compiler).

You probably want something like this

public func += ( left:  inout CGVector, right: CGVector) {
left = CGVector(dx: left.dx + right.dx, dy: left.dy + right.dy)
}

Var getter and setter confusion

You can create a computed property, backed by a stored property - I used this pattern in C# a lot:

private var _plane: SKSpriteNode
public var plane: SKSpriteNode { get { return _plane } set { _plane = newValue } }

note however that you achieve the same by just using one stored property:

public var plane: SKSpriteNode

It's the same happening in obj-c with the @synthesize statement (now obsolete because automatically applied). The difference is that the backing data member is available to the class (usually with property name prefixed by the underscore), whereas in swift that data member is not visible at all.

Addendum Wondering if I have misunderstood your question. Are you maybe be looking for a way to create a property whose value is calculated? If yes, then use a computer property:

public var plane: SKSpriteNode { get { return ... } }

Swift: Why does a variable with a setter must also have a getter?

Quick answer for the quick question "Why?":

Once you add a get (or set) to a property, it turns into a Computed Property. Computed properties may be read-only, thus requiring only a getter, but they may not be write-only. So if you add a setter to a property, you turn it into a computer property hence you must add a getter as well.

EDIT: Following comments, Why can Computed Properties be read-only, but not write-only?

I can't find official documentation to back this up, so the following explanation is solely based on my logical point of view:

Computed Properties don't store actual data on their variables; instead they compute data to display.

var a:Int = 5 // Stored property
var timesTen:Int { // Computed Property
get {
return a * 10
}
}

From the example above: a is a Stored property. Therefore, its get method automatically returns a's value. Now think of timesTen: What's its value? Since it doesn't store any value in timesTen variable, you must tell the computed property how and where to read its "value" from. So that's why you can't use a computed property for writing-only purposes.


Workaround / How to avoid it

For simple properties you may use didSet or willSet to achieve the desired purpose:

var proxy: MyProxy?
{
willSet { if _proxy == nil && newValue != nil { _proxy = newValue } }
}

If you are using Swift 2.x, you may use the guard statement to for a cleaner code:

var proxy: MyProxy?
{
willSet {
guard (_proxy == nil && newValue != nil) else { return }
_proxy = newValue
}
}

Another observation

If _proxy is not required to be private you may remove it completely, using only one variable/property instead of two:

var proxy: MyProxy!
{
willSet {
guard (proxy == nil && newValue != nil) else { return }
// By not preventing `willSet` to continue, `newValue` will automatically assigned to `proxy`
}
}

Swift implement setter without getter

You can use the property observer didSet. This will be called immediately after setting the value.

var selectedIndex: Int {
didSet {
selectItemAtIndex(selectedIndex)
}
}

Setter method giving underscore error when I have a getter method

It should be

return _lastSyncDate;

in the getter method. By default, the synthesized instance variables have a leading underscore
(and you already use that in the setter method).

Also, if you provide both setter and getter method, you have to synthesize the
property explicitly:

@synthesize lastSyncDate = _lastSyncDate;

The same would happen if you provide the getter for a read-only property.
The property is synthesized by the compiler only if a
required accessor method is missing.



Related Topics



Leave a reply



Submit