【问题标题】:computed setter for a subscript of an array in Swift [duplicate]Swift中数组下标的计算设置器[重复]
【发布时间】:2014-12-28 09:45:37
【问题描述】:

为了简短起见,我想要实现的例如:

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
      }
    }
}

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

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

编辑:

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

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

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

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

编辑 2:

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

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(){

    }
}

【问题讨论】:

  • Swift 数组 do 允许通过下标访问成员。
  • 你自己试过了吗?我相信你会说不然
  • Apple 的 documentation 另有说明。
  • LOL,所以我想您还没有尝试过相同的代码?这是 XCode 生成的确切错误:Expected 'get', 'set', 'willSet', or 'didSet' keyword to start an accessor definition

标签: xcode swift


【解决方案1】:

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

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"

【讨论】:

  • 首先感谢您的回答。我想我想要实现的是进行一些计算,例如一旦将actions[i] 设置为newValue,就重新定位actions[i] 的相应子视图。但在这种情况下,不会调用数组的计算设置器。对吗?
  • 嗯,这更有意义。我会考虑的。
  • 我已经更新了我的答案,这是您要找的吗?
  • 正是我正在寻找的东西,在我发布问题几个小时后,我想我可能必须这样做。无论如何,非常感谢
【解决方案2】:

以下应该有效:

var actions: [String] = []

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

    set(newValue)
    {
        assert(index < actions.count && index >= 0, "Index out of range")
        actions[index] = newValue
    }
}

【讨论】:

  • 我已经更新了我的问题,请查看
  • 这个答案不行,因为我必须调用 view[3] 来调用你的下标设置器,这是不正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 2014-11-17
  • 1970-01-01
  • 2012-11-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多