Swift-enum
2020-11-18 本文已影响0人
守护地中海的花
OC
typedef NS_ENUM(NSUInteger, APPCustomTabBarClickType) {
APPCustomTabBarHomePage,
APPCustomTabBarActivityPage,
APPCustomTabBarCartPage,
APPCustomTabBarMinePage,
};
Swift
enum Direction {
case north
case south
case east,west
}
var dir = Direction.west
dir = Direction.east
dir = .north;
print(dir)
原始值 Raw Values
enum PokerSuit :Character {
case spade = "🖤"
case heart = "♥️"
case diamond = "♦️"
case club = "♣️"
}
var suit = PokerSuit.spade
print(suit,suit.rawValue)
隐式原始值 Implicitly Assigned Raw Values
- 如果枚举的原始值类型是Int、String会自动分配原始值
递归枚举 Recursive Enumeration
后面搞
MemoryLayout
CaseIterable
enum PokerSuit :Character,CaseIterable {
case spade = "🖤"
case heart = "♥️"
case diamond = "♦️"
case club = "♣️"
}
let pokers = PokerSuit.allCases
for i in pokers {
print(i)
}
- 让枚举遵守CaseIterable协议,可以实现遍历枚举值