这可以在Swift中创建一个元组枚举吗?
我想建立类似的东西:
enum ErrorCode: (Int, String) {
case Generic_Error = (0, "Unknown")
case DB_Error = (909, "Database")
}
但是它没有编译......有没有办法获得类似的结果?
答案 0 :(得分:52)
Swift枚举不能将元组作为原始值类型。
替代方法包括存储代码并从中获取描述:
enum ErrorCode: Int, CustomStringConvertible {
case Generic = 0
case DB = 909
var description: String {
switch self {
case .Generic:
return "Unknown"
case .DB:
return "Database"
}
}
}
...或在枚举案例中存储代码和描述的关联值:
enum Error {
case Generic(Int, String)
case DB(Int, String)
}
如果您只是寻找不变的值,@ matt建议在struct
内组织它们也会有效。
答案 1 :(得分:41)
这取决于你所说的“相似”。我所做的是使用具有静态常量属性的Struct:
struct Trouble {
static let Generic_Error = (0, "Unknown")
static let DB_Error = (909, "Database")
}
现在,Trouble.Generic_Error
之类的内容可用于整个代码。
答案 2 :(得分:25)
您可以符合RawRepresentable
并使用(Int, String)
作为Self.RawValue
相关类型。
enum ErrorCode: RawRepresentable {
case Generic_Error
case DB_Error
var rawValue: (Int, String) {
switch self {
case .Generic_Error: return (0, "Unknown")
case .DB_Error: return (909, "Database")
}
}
init?(rawValue: (Int, String)) {
switch rawValue {
case (0, "Unknown"): self = .Generic_Error
case (909, "Database"): self = .DB_Error
default: return nil
}
}
}
答案 3 :(得分:16)
你可以这样做,也许:
enum ErrorCode {
case Generic_Error
case DB_Error
func values() -> (code: Int!, description: String?)! {
switch self {
case .Generic_Error:
return (0, "Unknown")
case .DB_Error:
return (909, "Database")
}
}
}
你可以稍后再做这件事:
let errorCode: ErrorCode = ErrorCode.Generic_Error;
if (errorCode.values().code == 0) {
// do the business here...
}
答案 4 :(得分:12)
创建您的Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Discussed Files").Select
Range("Table1[#Headers]").Select
Selection.Copy
Sheets("All data").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Discussed Files").Select
Range("Table1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All data").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Files within 3 Days").Select
Range("Table3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All data").Select
Range("A" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Files 10.04.17").Select
Range("Table5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All data").Select
Range("A" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$Y$" & lastRow), , xlYes).Name = _
"Table14"
Range("Table14[#All]").Select
ActiveSheet.ListObjects("Table14").TableStyle = "TableStyleMedium2"
并添加enum
元组类型var
。
(String, String)
以后:
enum SomeType {
case type1
case type2
case type3
var typeNameAndDescription: (name: String, description: String) {
switch self {
case .type1:
return ("type1 name", "type1 description")
case .type2:
return ("type2 name", "type2 description")
case .type3:
return ("type3 name", "type3 description")
}
}
}
答案 5 :(得分:1)
enum ErrorCode {
case Generic_Error
case DB_Error
public var code:Int{
switch self {
case .Generic_Error: return 0
case .DB_Error: return 909
}
}
public var name:String{
switch self {
case .Generic_Error: return "Unknown"
case .DB_Error: return "Database"
}
}
}
使用:
let err:ErrorCode = .Generic_Error
print(err.code) //0
print(err.name) //Unknown
答案 6 :(得分:0)
我保留枚举的解决方案是为rawValue var创建get方法:
enum LoadingType {
case poster
case movie
case refresh
var rawValue: (file: String, anim: String) {
get {
switch self {
case .movie:
return ("poster_loading", "LoadingView")
case .poster:
return ("loading", "LoadingView")
case .refresh:
return ("loading", "RefreshView")
}
}
}
}
使用此代码,您甚至可以通过名称调用每个Tuple元素:
self.type.rawValue.file
答案 7 :(得分:0)
我想我会将您的代码更改为以下内容:
enum ErrorCode {
case generic, db
var message: String {
switch self {
case .generic:
return "Unknown"
case .db:
return "Database"
}
}
var code: Int {
switch self {
case .generic:
return 0
case .db:
return 909
}
}
}
我觉得这样会更容易使用:
let error = ErrorCode.generic
print("Error Code: \(error.code), Message: \(error.message)")
答案 8 :(得分:0)
这是我在类似情况下所做的。使用具有默认值的关联数据枚举(如果愿意,您甚至可以在元组中命名值)。然后添加一个简单的函数,该函数基于self返回元组。
<button style="background:orange" onclick="undo()">UnDo Button</button>
<br><br><br>
<p id="dv"></p>
<br>
<p id="dvv"></p>
以后
enum ErrorCode {
case Generic_Error( code: Int = 0, desc: String = "Unknown")
case DB_Error( code: Int = 909, desc: String = "Database")
func getCodeDescription() -> (Int, String) {
switch self {
case let .Generic_Error(code, desc): return (code, desc)
case let .DB_Error(code, desc): return(code, desc)
}
}
}
答案 9 :(得分:-4)