使用自定义 setter 方法为 Swift 中的对象设置依赖 属性 变量
Set dependent property variable with custom setter method for object in Swift
我有一个 class 对象 属性 命名对象,和 一个名为 isEmpty 的布尔只读变量。如果对象 属性 设置为某个对象,则 isEmpty 属性 切换为 false,如果对象设置为 nil,则 isEmpty 设置为 true。我可以使用 Objective-C 实现这一点,如下所示:
@interface SomeObject: NSObject
@property (nonatomic, strong) id object;
@property (nonatomic, assign, readonly) BOOL isEmpty;
@end
@implementation SomeObject
- (void)object:(id)object {
_isEmpty = NO;
_object = object;
}
- (id)object {
return _object;
}
- (void)clearObject {
_object = nil;
_isEmpty = YES;
}
@end
如何在 Swift 2.2 中模仿它?到目前为止我已经试过了
class SomeObject: NSObject {
public private(set) var isEmpty: Bool
var nodeObject: AnyObject? = {
get {
isEmpty = false
return object
}
set(_object: AnyObject) {
object = _object
}
}
}
但正如预期的那样,它不起作用!我知道这是错误的,但不确定是哪一部分!
TIA
这对你有用吗?:
class SomeObject: NSObject {
var isEmpty: Bool
var nodeObject: AnyObject? {
didSet {
if nodeObject != nil {
isEmpty = false
} else {
isEmpty = true
}
}
}
}
除非你绝对需要设置isEmpty,否则我会这样做:为isEmpty创建一个getter并检查getter中的nodeObject是否为null:
class SomeObject {
var isEmpty: Bool {
get {
return nodeObject == nil
}
}
var nodeObject: AnyObject?
}
参见fiddle:http://swiftlang.ng.bluemix.net/#/repl/57c73513458cfba6715ebebd
编辑:如果你真的想让它接近 Objective-C,我建议这个版本:
class SomeObject {
private var _isEmpty: Bool = true
var isEmpty: Bool {
get {
return _isEmpty
}
}
var nodeObject: AnyObject? {
didSet {
_isEmpty = nodeObject == nil
}
}
}
Fiddle: https://swiftlang.ng.bluemix.net/#/repl/57c738b646a02c0452203f98
尝试使用 SWIFT 2 版本:
class SomeObject: NSObject {
var object: AnyObject? {
return object
}
private(set) var isEmpty = false
func object(object: AnyObject) {
self.isEmpty = false
self.object = object!
}
func clearObject() {
self.object = nil
self.isEmpty = true
}
}
我有一个 class 对象 属性 命名对象,和 一个名为 isEmpty 的布尔只读变量。如果对象 属性 设置为某个对象,则 isEmpty 属性 切换为 false,如果对象设置为 nil,则 isEmpty 设置为 true。我可以使用 Objective-C 实现这一点,如下所示:
@interface SomeObject: NSObject
@property (nonatomic, strong) id object;
@property (nonatomic, assign, readonly) BOOL isEmpty;
@end
@implementation SomeObject
- (void)object:(id)object {
_isEmpty = NO;
_object = object;
}
- (id)object {
return _object;
}
- (void)clearObject {
_object = nil;
_isEmpty = YES;
}
@end
如何在 Swift 2.2 中模仿它?到目前为止我已经试过了
class SomeObject: NSObject {
public private(set) var isEmpty: Bool
var nodeObject: AnyObject? = {
get {
isEmpty = false
return object
}
set(_object: AnyObject) {
object = _object
}
}
}
但正如预期的那样,它不起作用!我知道这是错误的,但不确定是哪一部分!
TIA
这对你有用吗?:
class SomeObject: NSObject {
var isEmpty: Bool
var nodeObject: AnyObject? {
didSet {
if nodeObject != nil {
isEmpty = false
} else {
isEmpty = true
}
}
}
}
除非你绝对需要设置isEmpty,否则我会这样做:为isEmpty创建一个getter并检查getter中的nodeObject是否为null:
class SomeObject {
var isEmpty: Bool {
get {
return nodeObject == nil
}
}
var nodeObject: AnyObject?
}
参见fiddle:http://swiftlang.ng.bluemix.net/#/repl/57c73513458cfba6715ebebd
编辑:如果你真的想让它接近 Objective-C,我建议这个版本:
class SomeObject {
private var _isEmpty: Bool = true
var isEmpty: Bool {
get {
return _isEmpty
}
}
var nodeObject: AnyObject? {
didSet {
_isEmpty = nodeObject == nil
}
}
}
Fiddle: https://swiftlang.ng.bluemix.net/#/repl/57c738b646a02c0452203f98
尝试使用 SWIFT 2 版本:
class SomeObject: NSObject {
var object: AnyObject? {
return object
}
private(set) var isEmpty = false
func object(object: AnyObject) {
self.isEmpty = false
self.object = object!
}
func clearObject() {
self.object = nil
self.isEmpty = true
}
}