简体   繁体   English

您如何对带有无关值的枚举案例进行简单的内联测试?

[英]How can you do a simple inline test for an enumeration's case that has associated values you don't care about?

Given this code: 给出以下代码:

class Item{}

func foo(item:Item){}

enum SelectionType{
    case single(resultsHandler:(Item)->Void)
    case multi(resultsHandler:([Item])->Void)
}

var selectionType:SelectionType = .single(resultsHandler:foo)

// This line won't compile
let title = (selectionType == .single)
    ? "Choose an item"
    : "Choose items"

How can you update the part that won't compile? 您如何更新不会编译的部分?

A ternary operator cannot work for enums with associated values, because the classic equality operator does not work with them. 三元运算符不能用于具有关联值的枚举,因为经典的相等运算符不能与它们一起使用。 You have to use pattern matching, or if case syntax: 您必须使用模式匹配,或者if case语法:

let title: String
if case .single = selectionType {
    title = "Choose an item"
} else {
    title = "Choose items"
}

I don't know if there is a direct way to address what you are looking for. 我不知道是否有直接的方法可以解决您的需求。

My understanding: 我的理解:

  • Enums are not equatable by default, you have to implement Equatable 默认情况下,枚举不可等于,您必须实现等于
  • For enums with associated values, even if you implemented it, I don't think it is possible to achieve what you had asked. 对于具有关联值的枚举,即使您实现了它,我也不可能实现您所要求的。

Computed variable (Workaround 1): 计算的变量(解决方法1):

enum SelectionType{

    case single(resultsHandler:(Item)->Void)
    case multi(resultsHandler:([Item])->Void)

    var title : String {

        let text : String

        switch self {

        case .single(_):
            text = "Choose an item"
        case .multi(_):
            text = "Choose items"
        }

        return text
    }
}

var title = selectionType.title

Use if case (Workaround 2): 如果情况适用(解决方法2):

if case .single(_) = selectionType {
    print("Choose an item")
}
else {
    print("Choose items")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从枚举案例的特定实例中提取关联值? - How can you extract associated values from a specific instance of an enum's case? 您如何在快速枚举的情况下为某些情况设置默认值,而为其他情况设置动态值? - How do you cases in swift enumeration that has default values for some cases and dynamic values for others? 如何在Swift 4中测试带有关联值的枚举案例的等价性 - How may I test the equivalency of enumeration cases with associated values in Swift 4 当您没有场景委托 (SwiftUI) 时,如何将.environment(...) 添加到 ContentView()? - How do you add .environment(...) to your ContentView() when you don't have a scene delegate (SwiftUI)? 关联值和原始值可以在 Swift 枚举中共存吗? - Can associated values and raw values coexist in Swift enumeration? 如何更改与swift编译器关联的macOS SDK - How do you change the macOS SDK associated with the swift compiler Swift:当您不知道键的名称或值的数据类型时如何使用 JSON - Swift: How to work with JSON when you don't know the name of the keys or the data type of the values Swift - 如何在点击按钮时测试标签是否已更新 - Swift - How do you test whether a label has been updated when a button is tapped 枚举具有关联值的枚举-Swift - Enumeration on enums with associated values - Swift 如何使用Alamofire测试证书固定? - How do you test certificate pinning with Alamofire?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM