Swift 中数组下标的计算 setter [英] computed setter for a subscript of an array in Swift

查看:39
本文介绍了Swift 中数组下标的计算 setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我想要实现的是:

To keep it short, what I want to achieve is for example:

var actions: [String]{
    get{
        if (_actions==nil){
            _actions = []
        }
        return _actions!
    }
    set{
        _actions = newValue
    }
    subscript(index:Int) -> String{
      set {
         assert(index<_actions.count && index>=0, "Index out of range")
         _actions[index] = newValue
      }
    }
}

我知道下标不是数组的访问器,但是最方便的替代方法是什么?

I know subscript isn't an accessor for array, but then what is the most convinient alternative to do just that?

如果可能的话,我真的很感谢您提供简洁的答案!非常感谢!

I truly appreciate for succinct answers if possible! Thank you very much!

为了扩展我对@jrturton 的解释,

To extend my explanation for @jrturton,

我想要实现的是,每当将 actions[i] 设置为 newValue 时,我都想做一些额外的计算,例如重新定位 actions[i] 的相应子视图.

What I am trying to achieve is whenever actions[i] is set to a newValue, I would like to do some extra computations, such as repositioning actions[i]'s respective subview.

但是如果我说 actions[3] = "randomMethod",整个数组的计算 setter 将被调用.对?因此,我想找到一种方法,例如,当 actions[3] 设置为 newValue 时,可以调用函数 repositionView(3).

But if i say actions[3] = "randomMethod", the computed setter for the entire array will get called. Right? So I'd like to find a way so that when actions[3] is set to a newValue, a function repositionView(3) can get called, for example.

我知道其他方法可以做到这一点,但我的问题只是询问是否有更方便的方法,如上面的示例:计算设置器,来做我想做的事?

I know other ways to do it, but my question simply askes if there is a more convinient way, like the example above: a computed setter, to do what I want?

编辑 2:

为了向@Vatsal Manot 展示我真正的意思,我删除了下标的 getter,这是一个完整的 example.swift(由于错误而无法运行):

To show @Vatsal Manot what I truly mean, I removed getter for subscript, and here is a complete example.swift(which wont run due to error):

import UIKit
import Foundation

class DWActionsSubmenu: UIView{
    var actions: [DWAction]{
        get{
            if (_actions==nil){
                _actions = []
            }
            return _actions!
        }
        set{
            _actions = newValue
        }
        subscript(index:Int) -> DWAction{
            set {
                assert(index<_actions.count && index>=0, "Index out of range")
                _actions[index] = newValue
                a()
            }
        }
    }

    var _actions: [DWAction]?

    init(actions:[DWAction]?){
        super.init()
        _actions = actions
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }

    func a(){

    }
}

推荐答案

我会将您的操作列表包装在一个自定义类中,然后您可以通过下标访问该类.然后,您可以添加一个块以在设置下标成员时运行:

I'd wrap your actions list in a custom class that you can then access via subscripting. You can then add a block to be run whenever a subscripted member is set:

class ActionList {
    private var actions = [String]()

    var actionDidChange : ((Int) -> ())?

    subscript(actionIndex:Int) -> String {
        get {
            return actions[actionIndex]
        }
        set {
            actions[actionIndex] = newValue
            if let actionDidChange = actionDidChange {
                actionDidChange(actionIndex)
            }
        }
    }

    func addAction(action: String) {
        actions.append(action)
    }

    func addActions(newActions:[String]) {
        actions += newActions
    }
}

用法(在操场上):

let actionList = ActionList()
actionList.actionDidChange = {
    actionIndex in
    println("Action \(actionIndex) did change")
}

actionList.addActions(["One", "Two", "Three"])
actionList[2] = "New"
// Prints "Action 2 did change"

这篇关于Swift 中数组下标的计算 setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆