swift - 具有关联值的枚举元素必须至少有一个关联值

标签 swift

这种用关联值定义枚举的语法在 Swift 4.2 中使用得很好

enum NetworkService {
    case lookUp(type: String)
    case allLookUps()
}

当我尝试在 Xcode 10.2 和 Swift 5 中转换我的代码时,它抛出错误提示

Enum element with associated values must have at least one associated value enter image description here

在这种特殊情况下,为 allLookUps() 提供关联值没有任何意义。那么 Swift 5 中的最佳实践是什么?

最佳答案

只需去掉 case 后面的括号 () 即可:

enum NetworkService {
    case lookUp(type: String)
    case allLookUps
}

关于swift - 具有关联值的枚举元素必须至少有一个关联值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56015677/

相关文章:

ios - UISearchController 问题 swift ios

ios - 如何在 swift 3 的其他功能中使用服务器响应和数据

iOS 推送通知仅由开发设备接收

swift - 使用 NSTextViewDelegate 和 textViewDidChange 不工作来监听 NSTextView 的变化

ios - 在 iOS 12 中,当使用具有约束的自调整大小单元格创建 UICollectionView 时,重新加载布局时高度错误,直到我滚动

iOS 不保存所有图像

ios - swift 2.0 : Best Practice for communication to a REST API with JSON

ios - Swift 使用 GCDAsyncSocket 创建套接字客户端

ios - 如何在 Swift 中调用另一个类的方法?

ios - 每次发生点击时重置 DispatchQueue