赞
踩
示例:
- enum Status {
- case success
- case failure(errorCode: Int)
- case loading(progress: Float)
- }
-
- let success = Status.success
- let failure = Status.failure(errorCode: 404)
- let loading = Status.loading(progress: 0.5)
示例:
- enum Direction: String {
- case north = "N"
- case south = "S"
- case east = "E"
- case west = "W"
- }
-
- let direction = Direction.north
- print(direction.rawValue) // 输出 "N"
示例:
- enum LinkedList<T> {
- case empty
- indirect case node(value: T, next: LinkedList<T>)
- }
-
- let list = LinkedList.node(value: 1, next: .node(value: 2, next: .empty))
示例:
- protocol Printable {
- var description: String { get }
- }
-
- enum Weekday: Printable {
- case monday, tuesday, wednesday, thursday, friday, saturday, sunday
-
- var description: String {
- switch self {
- case .monday:
- return "Monday"
- case .tuesday:
- return "Tuesday"
- case .wednesday:
- return "Wednesday"
- case .thursday:
- return "Thursday"
- case .friday:
- return "Friday"
- case .saturday:
- return "Saturday"
- case .sunday:
- return "Sunday"
- }
- }
- }
-
- let today = Weekday.monday
- print(today.description) // 输出 "Monday"

这些是Swift中枚举的一些高级用法。根据实际需求,你可以使用这些特性来更好地组织和处理数据。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。