Swift|Swift 枚举
enum ApiType {
case home
case userInfo(userId:String)
}var apiType = ApiType.userInfo(userId: "12345")if case let ApiType.userInfo(userId) = apiType {
// "12345"
print(userId)
}apiType = ApiType.homeif case ApiType.home = apiType {
// "ApiType.home"
print("ApiType.home")
}switch apiType {
case .home:
print("home")
case .userInfo("12345"):
print("userInfo(\"12345\")")
case let .userInfo(userId):
print("userInfo \(userId)")
}
注意
case
和case let
,如果只想处理部分情况,可以考虑使用 if case
进行匹配,而不需要写switch
。rawValue
枚举类型和基础类型进行比较时,可以考虑使用 rawValue。
enum Direction : Int {
// e = 0
case e
case s = 3
// w = 4
case w
case n = 9
}enum Name : String {
case Tom
case Tony = "122"
// Ted = "Ted"
case Ted
}
RawRepresentable
【Swift|Swift 枚举】在声明枚举的时候,可以选择遵循 RawRepresentable 协议。
public protocol RawRepresentable {
// 被associatedtype关键字修饰的变量,具体的类型需要让实现的类来指定
associatedtype RawValueinit?(rawValue: Self.RawValue)var rawValue: Self.RawValue { get }
}// ExpressibleByStringLiteral
//
struct MyApi : Equatable,ExpressibleByStringLiteral {
var url : String?static func == (obj1:MyApi,obj2:MyApi) -> Bool {
if obj1.url == nil && obj2.url == nil {
return true
}if obj1.url == nil || obj2.url == nil {
return false
}if obj1.url!.elementsEqual(obj2.url!) {
return true
} else {
return false
}
}init(stringLiteral value: StringLiteralType) {
self.url = value
}
}enum MyApiEnum : MyApi,RawRepresentable {
typealias RawValue = https://www.it610.com/article/MyApicase home, detail
case unknown// Enum with raw type cannot have cases with arguments
// case userInfo(userId:String)var rawValue: RawValue {
switch self {
case .home:
return MyApi("123")
case .detail:
return MyApi("234")
default:
return MyApi("xxxx")
}
}init?(rawValue: MyApi) {
switch rawValue {
case MyApi("123") : self = MyApiEnum.home
case MyApi("234") : self = MyApiEnum.detail
default:
self = MyApiEnum.unknown
}
}
}let kk = MyApiEnum.detail.rawValue// MyApi(url: Optional("234"))
print(kk)
推荐阅读
- Swift中willSet和didSet的简述
- Java基础-高级特性-枚举实现状态机
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- LeetCode算法题-11.|LeetCode算法题-11. 盛最多水的容器(Swift)
- iOS-Swift-map|iOS-Swift-map filter reduce、函数式编程
- Swift|Swift ----viewController 中addChildViewController
- SwiftUI|SwiftUI iOS 瀑布流组件之仿CollectionView不规则图文混合(教程含源码)
- Swift高级应用|Swift高级应用 -01
- Swift5.0|Swift5.0 UITexview的基本使用
- Swift7|Swift7 - 循环、函数