我试图从原始值获取枚举类型:
enum TestEnum: String {
case Name
case Gender
case Birth
var rawValue: String {
switch self {
case .Name: return "Name"
case .Gender: return "Gender"
case .Birth: return "Birth Day"
}
}
}
let name = TestEnum(rawValue: "Name") //Name
let gender = TestEnum(rawValue: "Gender") //Gender
但似乎rawValue
对包含空格的字符串不起作用:
let birth = TestEnum(rawValue: "Birth Day") //nil
有任何建议如何获得它?
答案 0 :(得分:97)
太复杂,只需将原始值直接分配给案例
enum TestEnum: String {
case Name = "Name"
case Gender = "Gender"
case Birth = "Birth Day"
}
let name = TestEnum(rawValue: "Name")! //Name
let gender = TestEnum(rawValue: "Gender")! //Gender
let birth = TestEnum(rawValue: "Birth Day")! //Birth
如果案例名称与原始值匹配,您甚至可以省略它
enum TestEnum: String {
case Name, Gender, Birth = "Birth Day"
}
在Swift 3+中,所有枚举案例都是lowercased
答案 1 :(得分:10)
完整的工作示例:
enum TestEnum: String {
case name = "A Name"
case otherName
case test = "Test"
}
let first: TestEnum? = TestEnum(rawValue: "A Name")
let second: TestEnum? = TestEnum(rawValue: "OtherName")
let third: TestEnum? = TestEnum(rawValue: "Test")
print("\(first), \(second), \(third)")
所有这些都可行,但在使用原始值初始化时,它将是可选的。如果这是一个问题,你可以为枚举创建一个初始化器或构造函数来尝试处理这个问题,添加none
个案例并在无法创建枚举的情况下返回它。像这样:
static func create(rawValue:String) -> TestEnum {
if let testVal = TestEnum(rawValue: rawValue) {
return testVal
}
else{
return .none
}
}
答案 2 :(得分:3)
你可以define enum这样 -
enum TestEnum: String {
case Name, Gender, Birth
}
或强>
enum TestEnum: String {
case Name
case Gender
case Birth
}
您可以提供 init 方法,其中默认为其中一个成员值。
enum TestEnum: String {
case Name, Gender, Birth
init() {
self = .Gender
}
}
在上面的示例中,TestEnum.Name的隐含原始值为" Name",依此类推。
使用其rawValue属性访问枚举大小写的原始值:
let testEnum = TestEnum.Name.rawValue
// testEnum is "Name"
let testEnum1 = TestEnum()
// testEnum1 is "Gender"
答案 3 :(得分:1)
使用 Swift 4.2 和 CaseIterable 协议,一点都不难!
这里是实现它的示例。
import UIKit
private enum DataType: String, CaseIterable {
case someDataOne = "an_awesome_string_one"
case someDataTwo = "an_awesome_string_two"
case someDataThree = "an_awesome_string_three"
case someDataFour = "an_awesome_string_four"
func localizedString() -> String {
// Internal operation
// I have a String extension which returns its localized version
return self.rawValue.localized
}
static func fromLocalizedString(localizedString: String) -> DataType? {
for type in DataType.allCases {
if type.localizedString() == localizedString {
return type
}
}
return nil
}
}
// USAGE EXAMPLE
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let dataType = DataType.fromLocalizedString(localizedString: self.title) {
loadUserData(type: dataType)
}
}
您可以轻松地对其进行修改,以基于rawValue返回DataType。希望对您有所帮助!
答案 4 :(得分:0)
以下是Swift 4.1中更多可用代码的示例
import UIKit
enum FormData {
case userName
case password
static let array = [userName, password]
var placeHolder: String {
switch self {
case .userName:
return AppString.name.localized // will return "Name" string
case .password:
return AppString.password.localized // will return "Password" string
}
}
}
enum AppString: String {
case name = "Name"
case password = "Password"
var localized: String {
return NSLocalizedString(self.rawValue, comment: "")
}
}
答案 5 :(得分:0)
使用枚举显示原始值
import UIKit
enum car: String {
case bmw = "BMW"
case jaquar = "JAQUAR"
case rd = "RD"
case benz = "BENZ"
}
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
label.text = car.bmw.rawValue
}
}
答案 6 :(得分:0)
我认为这是Swift 4.2(您可以C&P到游乐场)的快速,干净的解决方案
import UIKit
public enum SomeEnum: String, CaseIterable {
case sun,moon,venus,pluto
}
let str = "venus"
let newEnum = SomeEnum.allCases.filter{$0.rawValue == str}.first
// newEnum is optional
if let result = newEnum {
print(result.rawValue)
}