重新初始化静态变量

问题描述 投票:3回答:2

我在Settings VC中有一个简单的tableview。有两种不同的情况,登录用户是员工或管理员。管理员可以将用户添加到组织中,因此他看到两个部分,每个部分一行(添加用户和注销),而普通员工只看到一个部分有一行(注销)。要管理indexPaths,我有以下结构:

struct Indexes {
    struct AddEmployee {
        static let row = 0
        static let section = isEmployee() ? -1 : 0
        static func indexPath() -> IndexPath {
            return IndexPath(row: row, section: section)
        }
    }

    struct SignOut {
        static let row = 0
        static let section = isEmployee() ? 0 : 1
        static func indexPath() -> IndexPath {
            return IndexPath(row: row, section: section)
        }
    }
}

在我的TableView委托中,我得到了以下方法:

func numberOfSections(in tableView: UITableView) -> Int {
    return isEmployee() ? 1 : 2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsTableViewCell", for: indexPath)
        if (indexPath == Indexes.AddEmployee.indexPath()) {
            cell.textLabel?.text = "Add Employee"
        } else if (indexPath == Indexes.SignOut.indexPath()) {
            cell.textLabel?.text = "Log out"
        }
        return cell
    }

现在的问题是,当我以Admin身份登出并以Employee身份登录(反之亦然)时,结构的static变量仍然被初始化,并且isEmployee()返回值已经改变的事实并不重要,因为变量不是没有重装,所以我的细胞数量错了。是否有可能强制结构重新加载其静态变量?

swift
2个回答
5
投票

没有办法强制结构重新加载它们的静态存储常量,但你只需要使用静态计算变量。

计算变量如下所示:

static var section: Int {
    get {
        return isEmployee() ? -1 : 0
    }
}

计算变量实际上并未存储,而是在每次调用时计算。除非您的逻辑需要很长时间才能执行,否则这不会成为问题。即使这被标记为var而不是let,它仍然是只读的,因为你只指定了一个getter(你可以在set块下面指定一个带有get块的setter)。

作为一种快捷方式,当你只想要一个getter时,你不需要显式地显示get关键字和大括号,所以你可以摆脱它们。你离开了:

struct Indexes {
    struct AddEmployee {
        static let row = 0
        static var section: Int {
            return isEmployee() ? -1 : 0
        }
        static func indexPath() -> IndexPath {
            return IndexPath(row: row, section: section)
        }
    }

    struct SignOut {
        static let row = 0
        static var section: Int {
            return isEmployee() ? 0 : 1
        }
        static func indexPath() -> IndexPath {
            return IndexPath(row: row, section: section)
        }
    }
}

如果你愿意,你甚至可以将indexPath转换为计算变量而不是函数,它会做同样的事情。 Swift中的一个常见约定是在不需要参数时使用计算变量,并且获得结果的逻辑很简单,因此不会花费很长时间。


3
投票

为什么不将那些静态lets更改为计算属性?

static var section: Int {
    return isEmployee() ? -1 : 0
}

现在每次引用section时,都会对表达式isEmployee() ? -1 : 0进行求值。

或者,您可以在用户登录时更新这些值。

© www.soinside.com 2019 - 2024. All rights reserved.