如何在另一个函数中使用枚举 switch 语句

how to use an enum switch statement in another function

第一次使用枚举开关,所以有几个问题。

我想像这样在 tableView 函数中使用此 switch 语句。首先,我是否在使用枚举开关之前声明变量打开?如果是这样,我是将 open 变量传递给开关还是使用新名称创建开关并传递 open 变量?三、如何接收开关的值?

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FCT") as! FoodCellTwo

    let each = resultss[indexPath.row]

    var open: GMSPlacesOpenNowStatus = each.openNowStatus

  enum open : Int {


        /** The place is open now. */
        case yes

        /** The place is not open now. */
        case no

        /** We don't know whether the place is open now. */
        case unknown
    }

    cell.nameLabel.text = each.name

    return cell
}

这是使用枚举的方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let status = openStatus // Get you open status or you could just use switch(openStatus)
        switch status {
        case .yes:
            cell.textLabel?.text = "This places is open"
        case .no:
            cell.textLabel?.text =  "This places is closed now"
        case .unknown:
            cell.textLabel?.text =  "No idea about open status"
        }
        return cell
    }

我建议你像这样在 GMSPlacesOpenNowStatus 上写一个扩展

extension GMSPlacesOpenNowStatus {
    func getStringTitle() -> String {
        switch self {
        case .yes:
            return "This places is open"
        case .no:
            return "This places is closed now"
        case .unknown:
            return "No idea about open status"
        }
    }
}

并像这样使用此扩展程序

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let status = openStatus 
        cell.textLabel?.text = status.getStringTitle()
        return cell
    }