Swift如何使用枚举来获取字符串值 [英] Swift how to use enum to get string value

查看:758
本文介绍了Swift如何使用枚举来获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有枚举:

enum NewProgramDetails: String {
    case Description = "Description", ToMode = "To Mode", From = "From", To = "To", Days = "Days"

    static let allValues = [Description, ToMode, From, To, Days]
}

我想使用这个枚举在我的单元格中显示依赖于indexPath:

I want to use this enum to display in my cell depend on indexPath:

cell.textLabel.text = NewProgramDetails.ToMode

错误:无法分配'ViewController.NewProgramDetails'类型的值输入'String?'

error: Cannot assign value of type 'ViewController.NewProgramDetails' to type 'String?'

如何使用枚举值将其分配给标签文本作为一个字符串?

How can I use enum values to assign it to label text as a string?

推荐答案

使用枚举的 rawValue / p>

Use the rawValue of the enum:

cell.textLabel.text = NewProgramDetails.ToMode.rawValue

这篇关于Swift如何使用枚举来获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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