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



第一次使用枚举开关,所以一些问题。

我想在表观视图函数中使用此Switch语句。首先,在使用枚举开关之前,我是否声明变量打开?如果是这样,我是将开放变量传递给开关还是用新名称创建开关,然后通过开放变量传递?第三,如何从开关中接收值?

    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
    }

最新更新