寒城攻略:Listo 教你 25 天学会 Swift 语言 - 21 Nested Types
来源:互联网 发布:手机抽签软件 编辑:程序博客网 时间:2024/05/20 16:35
//***********************************************************************************************
//1.Nested Types(类型嵌套)
//_______________________________________________________________________________________________
//介绍
//枚举类型常被用于实现特定类或者结构体的功能。我们可以在多种变量环境中方便的定义通用的结构体或者类,Swift支持枚举,类,结构体为支持嵌套类型
//***********************************************************************************************
//2.Nested Types in Action(类型嵌套实例)
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 theAceOfSpades =BlackjackCard(rank: .Ace, suit: .Spades)
println("the AceOfSpades:\(theAceOfSpades.description)") //尽管 Rank 和 Suit 嵌套在 BlackjackCard中,但仍可被引用,所以在初始化实例时能够通过枚举类型中的成员名称(.Ace和 .Spades)单独引用
//***********************************************************************************************
//3.Referring to Nested Types(类型嵌套的引用)
//_______________________________________________________________________________________________
//实例代码演示类型嵌套的引用
//在外部对嵌套类型的引用,是以被嵌套类型的名字为前缀,加上所要引用的属性名
let heartsSymbol =BlackjackCard.Suit.Hearts.rawValue
println(heartsSymbol)
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 21 Nested Types
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 21 Nested Types
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 06 Collection Types
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 25 Listo's Conclusion
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 01 About Swift
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 02 Swift Tour
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 03 The Basic
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 04 Basic Operators
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 05 Strings and Characters
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 07 Control Flow
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 08 Functions
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 09 Closures
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 10 Enumerations
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 11 Class and Structures
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 12 Properties
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 13 Methods
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 14 Subscripts
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 15 Inheritance
- URAL 2023. Donald is a postman (预处理)
- C语言union的一个例子的讨论
- 设计模式学习
- ID3算法思想解析
- 使用NFC-SD手机和SD_SWP卡进行移动支付
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 21 Nested Types
- 安卓第四记
- zencart建站后台设计介绍
- java中实现js中的alert功能
- 96. 通用字段修改器
- 银行家舍入VS四舍五入
- leetcode Letter Combinations of a Phone Number
- 欢迎使用CSDN-markdown编辑器
- android 多屏幕适配