Shorthand setter declaration for a subscript of an array in Swift

情到浓时终转凉″ 提交于 2019-12-02 07:19:42

问题


In my last question, I asked how to write setter for a subscript of a computed property in Swift. I think my question was not substantial enough to be understood. The answer given was either incorrect or complicated for a small task. After a long thought, I still think maybe a brighter man can offer a more inspiring answer.

To subside confusion, my question is if there is a shorthand setter declaration for a subscript of an array in Swift. Because there is shorthand setter declaration for an array in swift, but not its subscript.

A shorthand getter/setter declaration is

var center: Point {
    get {
        let centerX = origin.x + (size.width / 2)
        let centerY = origin.y + (size.height / 2)
        return Point(x: centerX, y: centerY)
    }
    set {
        origin.x = newValue.x - (size.width / 2)
        origin.y = newValue.y - (size.height / 2)
    }
}

Basically, in a scenario where a set action for action[i] will cause actionButton[i] to be updated. There are basically two ways to do this quickly.

First solution

func setActionAtIndex(index:Int, forValue newValue:SomeClass){
    action[index] = newValue
    self.updateActionButtonAtIndex(index)
}

This solution above is easy to understand, however, it requires a function, which takes two lines of code, in one class. Not quite "Swift".

Second solution

var action: [SomeClass] {
    subscript(index:Int){
        set(index:Int,newValue:SomeClass){
           action[index] = newValue
           //extra action to be performed 
           updateActionButtonAtIndex(index)

        }
        get{
           return action[index]
        }
    }
}

Needless to say, this is absolutely wrong and this solution is nonexistent.

Why it is wrong?

Expected 'get', 'set', 'willSet', or 'didSet' keyword to start an accessor definition

Hence, is there a solution that is similar to second solution but works?


回答1:


I did think of another way, but I don't like it much. Use an extension to add another subscript to setter to Array - a setter that allows you to supply a callback. The problem with this approach is that you have remember to supply the callback! This puts the onus on whoever sets into the index, which is probably not what you had in mind. Still, it has the advantage, once configured, of brevity - and it takes O(0) time:

extension Array {
    subscript(ix:Int, f:(T,T,Int)->Void) -> T {
        set {
            let old = self[ix]
            self[ix] = newValue
            f(old,newValue,ix)
        }
        get {
            return self[ix]
        }
    }
}

class Thing {
    var arr : [Int] = [1, 2, 3]
    func f (oldv:Int, newv:Int, ix:Int) {
        println("changed \(oldv) to \(newv) at \(ix)")
    }
}

var t = Thing()
t.arr[1,t.f] = 100 // "changed 2 to 100 at 1"



回答2:


What you're not understanding is that an ordinary setter is the shorthand you are looking for. If an object has an array property and someone sets into that array, the setter is called.

For example:

class Thing {
    var arr : [Int] = [1, 2, 3] {
        didSet {
            println("I saw that")
        }
    }
}

var t = Thing()
t.arr[1] = 100 // "I saw that"

So all you have to do is use didSet to compare the new value to the old value and respond however you like to the change.



来源:https://stackoverflow.com/questions/28006010/shorthand-setter-declaration-for-a-subscript-of-an-array-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!