Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将Int传递给enum并返回字符串_Ios_Swift_Enums - Fatal编程技术网

Ios 将Int传递给enum并返回字符串

Ios 将Int传递给enum并返回字符串,ios,swift,enums,Ios,Swift,Enums,我试图完成一项任务,将整数值传递给enum,并为传入的整数返回一个特定字符串 我使用enum是因为整数是已知的,并且每个整数都有一个含义。我已经做了以下工作: enum Genre: String { case 28 = "Action" case 12 = "Adventure" case 16 = "Animation" case 35 = "Comedy" case 80 = "Crime" } 我期望的是:当传递一个案例时,我希望返回字符串关联

我试图完成一项任务,将整数值传递给enum,并为传入的整数返回一个特定字符串

我使用enum是因为整数是已知的,并且每个整数都有一个含义。我已经做了以下工作:

enum Genre: String {
    case 28 = "Action"
    case 12 = "Adventure"
    case 16 = "Animation"
    case 35 = "Comedy"
    case 80 = "Crime"
}
我期望的是:当传递一个案例时,我希望返回字符串关联


如果您有问题或需要进一步了解,请在评论中提问。

我建议创建一个实现所需映射的词典,并为密钥创建常量以使用它们

您可以首先创建一个名为Constants的类,并将以下常量放入其中:

static let action = 28
static let adventure = 12
// ... The rest of your constants.

// Then create a dictionary that contains the values:
static let genre = [action : "Action", adventure : "Adventure"] // And so on for the rest of your keys.
然后,您可以使用该字典访问所需的任何值,如下所示:

let actionString = Constants.genre[Constants.action]

希望这能有所帮助。

我建议创建一个实现所需映射的字典,并为键创建常量以使用它们

您可以首先创建一个名为Constants的类,并将以下常量放入其中:

static let action = 28
static let adventure = 12
// ... The rest of your constants.

// Then create a dictionary that contains the values:
static let genre = [action : "Action", adventure : "Adventure"] // And so on for the rest of your keys.
然后,您可以使用该字典访问所需的任何值,如下所示:

let actionString = Constants.genre[Constants.action]
希望这有帮助。

这个怎么样

enum Genre: Int {
    case action = 28
    case adventure = 12
    case animation = 16
    case comedy = 35
    case crime = 80
}
像这样使用它

// enum as string 
let enumName = "\(Genre.action)" // `action`

// enum as int value 
let enumValue = Genre.action.rawValue // 28

// enum from int
let action = Genre.init(rawValue: 28)
希望能有帮助。谢谢

这个怎么样

enum Genre: Int {
    case action = 28
    case adventure = 12
    case animation = 16
    case comedy = 35
    case crime = 80
}
let Genre = [28:"action",
12: "adventure",
16: "animation",
35: "comedy",
80: "crime"]
像这样使用它

// enum as string 
let enumName = "\(Genre.action)" // `action`

// enum as int value 
let enumValue = Genre.action.rawValue // 28

// enum from int
let action = Genre.init(rawValue: 28)
希望能有帮助。谢谢

let Genre = [28:"action",
12: "adventure",
16: "animation",
35: "comedy",
80: "crime"]
示例用法: 让retValue=Genre[28]//动作

以下是操场演示:

示例用法: 让retValue=Genre[28]//动作

以下是操场演示:

我们不能将Int作为枚举案例名称。 试试这个:

enum Genre: Int {
case action = 28, adventure = 12, animation = 16, comedy = 35, crime = 80

  func getString() -> String {
    switch self {
    case .action: return "Action"
    case .adventure: return "Adventure"
    case .animation: return "Animation"
    case .comedy: return "Comedy"
    case .crime: return "Crime"
    }
  }
}

let gener = Genre.action
print(gener.getString())//"Action"
如果您只知道整数值,请执行以下操作:

let gener1 = Genre(rawValue: 12)!
print(gener1.getString())//"Adventure"
我们不能将Int作为枚举案例名称。 试试这个:

enum Genre: Int {
case action = 28, adventure = 12, animation = 16, comedy = 35, crime = 80

  func getString() -> String {
    switch self {
    case .action: return "Action"
    case .adventure: return "Adventure"
    case .animation: return "Animation"
    case .comedy: return "Comedy"
    case .crime: return "Crime"
    }
  }
}

let gener = Genre.action
print(gener.getString())//"Action"
如果您只知道整数值,请执行以下操作:

let gener1 = Genre(rawValue: 12)!
print(gener1.getString())//"Adventure"

有没有不使用字典的理由?没有,有没有关于在这里使用字典的最佳做法的想法?@MEnnabah你最好使用字典有没有理由不使用字典?没有,有没有关于在这里使用字典的最佳做法的想法?@MEnnabah你最好使用字典