级别: ★☆☆☆☆
标签:「iOS」「Swift 5.1 」「嵌套类型」
作者: 沐灵洛
审校: QiShare团队
Swift允许定义嵌套类型,在它们所支持类型的定义中支持嵌套枚举,类和结构。类型可以嵌套到所需的级别。
struct BlackjackCard { // nested Suit enumeration enum Suit: Character { case spades = "♠", hearts = "♡", diamonds = "♢", clubs = "♣" } // nested Rank enumeration enum Rank: Int { case two = 2, three, four, five, six, seven, eight, nine, ten case jack, queen, king, ace struct Values { let first: Int, second: Int? } var values: Values { switch self { case .ace: return Values(first: 1, second: 11) case .jack, .queen, .king: return Values(first: 10, second: nil) default: return Values(first: self.rawValue, second: nil) } } } // BlackjackCard properties and methods let rank: Rank, suit: Suit var description: String { var output = "suit is \(suit.rawValue)," output += " value is \(rank.values.first)" if let second = rank.values.second { output += " or \(second)" } return output } } 复制代码
###嵌套类型的引用
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue // heartsSymbol is "♡" 复制代码
了解更多iOS及相关新技术,请关注我们的公众号:
关注我们的途径有:
QiShare(简书)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公众号)
推荐文章:
Swift 5.1 (17) - 类型转换与模式匹配 浅谈编译过程
深入理解HTTPS 浅谈 GPU 及 “App渲染流程”
iOS 查看及导出项目运行日志
Flutter Platform Channel 使用与源码分析
开发没切图怎么办?矢量图标(iconFont)上手指南
DarkMode、WKWebView、苹果登录是否必须适配?
奇舞团安卓团队——aTaller
奇舞周刊