为我的变量编写自定义Setter - Swift

时间:2014-08-27 14:09:41

标签: ios objective-c properties swift setter

我试图在属性发生变化时运行一个Setter,这也会影响该类中的逻辑:

以下是它在Objective-C中的用法:

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;

为fetchedResultsController设置方法:

- (void)setFetchedResultsController:(NSFetchedResultsController *)newFetchResultsContrller
{
    NSFetchedResultsController *oldfetchResultsController = _fetchedResultsController;
    if (newFetchResultsContrller != oldfetchResultsController) {
        _fetchedResultsController = newFetchResultsContrller;
        newFetchResultsContrller.delegate = self;
        if ((!self.title || [self.title isEqualToString:oldfetchResultsController.fetchRequest.entity.name]) && (!self.navigationController || !self.navigationItem.title)) {
            self.title = newFetchResultsContrller.fetchRequest.entity.name;
        }
        if (newFetchResultsContrller) {

            [self performFetch];
        } else {

            [self.tableView reloadData];
        }
    }
}

这是我在Swift中的尝试。但是我无法获取newFetchResultsController,因此无法完成休息。这是在swift中设置属性的正确方法吗?我怎样才能采用与Swift中的Objective C中所示相同的原理?

var fetchedResultsController:NSFetchedResultsController! {
    willSet {
        println("Set fetchedResultsController Called")
        self.fetchedResultsController.delegate = self
        performFetch()
    }
}

2 个答案:

答案 0 :(得分:6)

<强> UPD:
今天使用Swift 3.0你的逻辑可以重新实现,你可能有一个普通的存储属性,只有didSet值观察者指定如下:

var fetchedResultsController:NSFetchedResultsController? {
    didSet(oldfetchResultsController) {
        // If value BEFORE it was set is not identical to value AFTER it was set.
        if oldfetchResultsController !== fetchedResultsController {
            // Let's assume there is such method.
            if fetchedResultsController.isBadAndNotSuitable == false {
                // Do your stuff here.
            } else {
                // If you want to override value that was set
                // without observers being called, you can just set
                // your property inside didSet like:
                fetchedResultsController = nil
                // Or, if you don't want to change value:
                fetchedResultsController = oldFetchedResultsController
                // this won't cause willSet to fire and your property
                // will preserve its value.
                // It's ok to override observers in subclasses, because
                // super's implementation will be called as well before.
            }
        } 
    }
}

与2014年一样:
你可以这样写:

var _fetchedResultsController:NSFetchedResultsController? = nil
var fetchedResultsController:NSFetchedResultsController? {
    get {
        return _fetchedResultsController
    }
    set (aNewValue) {
        if (_fetchedResultsController != aNewValue)
        {
            _fetchedResultsController = aNewValue
        }
    }
}

使用此方法可以防止设置值。因此,例如,如果NSFetchedResultsController上有一个名为isBadAndNotSuitable的属性,您可以写:

set (aNewValue) {
    if (aNewValue.isBadAndNotSuitable)
    {
        NSLog("Don't set, it's bad")
    }
    else
    {
        _fetchedResultsController = aNewValue
    }
}

答案 1 :(得分:4)

willSet内,您有一个名为newValue的隐藏变量,您可以像在obj-c中一样使用它,如下所示:

willSet {
    if newValue != fetchedResultsController {
        //do stuff
    }
} 

newValue变量包含您刚刚尝试分配给该属性的值。