Skip to main content
Code Review

Return to Question

Fixed minor mistake
Source Link
rmaddy
  • 195
  • 7

There a various examples in UIKit where a class has a property and a corresponding method to set the property along with an animated property.

Examples include the progress and setProgress(_:animated:) of UIProgressView or the isEditing and setEditing(_:animated:) of UITableView.

In Objective-C you implement this by overriding the plain property setter to call the additional setter with the additional parameter:

- (void)setEditing:(BOOL)editing {
 [self setEditing:editing animated:NO];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
 _editing = editing // set the instance variable
 // The rest of the code
}

The question is about doing this in Swift. The best I can come up requires using a private backing variable for the public computed property in addition to the corresponding animated setter.

private var _editing: Bool
public var isEditing: Bool {
 get { return _editing }
 set { setEditing(newValue, animated: truefalse) }
}
public func setEditing(_ editing: Bool, animated: Bool) {
 _editing = editing
 // The rest of the code
}

Is there a better way to implement this pattern in Swift without the need to wrap a private property with a computed property à la Objective-C?

There a various examples in UIKit where a class has a property and a corresponding method to set the property along with an animated property.

Examples include the progress and setProgress(_:animated:) of UIProgressView or the isEditing and setEditing(_:animated:) of UITableView.

In Objective-C you implement this by overriding the plain property setter to call the additional setter with the additional parameter:

- (void)setEditing:(BOOL)editing {
 [self setEditing:editing animated:NO];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
 _editing = editing // set the instance variable
 // The rest of the code
}

The question is about doing this in Swift. The best I can come up requires using a private backing variable for the public computed property in addition to the corresponding animated setter.

private var _editing: Bool
public var isEditing: Bool {
 get { return _editing }
 set { setEditing(newValue, animated: true) }
}
public func setEditing(_ editing: Bool, animated: Bool) {
 _editing = editing
 // The rest of the code
}

Is there a better way to implement this pattern in Swift without the need to wrap a private property with a computed property à la Objective-C?

There a various examples in UIKit where a class has a property and a corresponding method to set the property along with an animated property.

Examples include the progress and setProgress(_:animated:) of UIProgressView or the isEditing and setEditing(_:animated:) of UITableView.

In Objective-C you implement this by overriding the plain property setter to call the additional setter with the additional parameter:

- (void)setEditing:(BOOL)editing {
 [self setEditing:editing animated:NO];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
 _editing = editing // set the instance variable
 // The rest of the code
}

The question is about doing this in Swift. The best I can come up requires using a private backing variable for the public computed property in addition to the corresponding animated setter.

private var _editing: Bool
public var isEditing: Bool {
 get { return _editing }
 set { setEditing(newValue, animated: false) }
}
public func setEditing(_ editing: Bool, animated: Bool) {
 _editing = editing
 // The rest of the code
}

Is there a better way to implement this pattern in Swift without the need to wrap a private property with a computed property à la Objective-C?

Source Link
rmaddy
  • 195
  • 7

Implementation of a property and a corresponding animated setter

There a various examples in UIKit where a class has a property and a corresponding method to set the property along with an animated property.

Examples include the progress and setProgress(_:animated:) of UIProgressView or the isEditing and setEditing(_:animated:) of UITableView.

In Objective-C you implement this by overriding the plain property setter to call the additional setter with the additional parameter:

- (void)setEditing:(BOOL)editing {
 [self setEditing:editing animated:NO];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
 _editing = editing // set the instance variable
 // The rest of the code
}

The question is about doing this in Swift. The best I can come up requires using a private backing variable for the public computed property in addition to the corresponding animated setter.

private var _editing: Bool
public var isEditing: Bool {
 get { return _editing }
 set { setEditing(newValue, animated: true) }
}
public func setEditing(_ editing: Bool, animated: Bool) {
 _editing = editing
 // The rest of the code
}

Is there a better way to implement this pattern in Swift without the need to wrap a private property with a computed property à la Objective-C?

default

AltStyle によって変換されたページ (->オリジナル) /