Files
SwiftStudy/05 枚举.playground/Contents.swift
2021-12-14 09:36:26 +08:00

143 lines
4.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
var greeting = "Hello, playground"
//: [Previous](@previous)
import Foundation
var greeting = "Hello, playground"
//: ####
//
enum Direction {
case north
case south
case east
case west
}
// 访
var dir = Direction.north
dir = Direction.east
dir = .north
print(dir)
//: #### Associated Values
//
enum Score {
case points(Int)
case grade(Character)
}
var score = Score.points(96)
score = .grade("A")
//: #### Raw Value
// 使
enum PokerSuit : Character { //
case spade = "♠️"
case heart = "♥️"
case dismand = "♦️"
case club = "♣️"
}
var suit = PokerSuit.spade
print(suit)
print(suit.rawValue) //
//: #####
/*:
IntStringSwift
- Int0
- String
*/
//: #### MemoryLayout
/*:
MemoryLayoutsizestridealignment
- size: . Size is the number of bytes to read from the pointer to reach all the data.
- stride: Stride is the number of bytes to advance to reach the next item in the buffer.The stride determines the distance between two elements, which will be greater than or equal to the size.
- alignmentThe alignment of a struct type is the maximum alignment out of all its properties.Alignment is the the evenly divisible by number that each instance needs to be at.
*/
var age = 18
MemoryLayout.size(ofValue: age)
MemoryLayout.alignment(ofValue: age)
MemoryLayout.stride(ofValue: age)
enum Password {
case other
case number(Int, Int, Int, Int)
}
MemoryLayout<Password>.size
MemoryLayout<Password>.stride
MemoryLayout<Password>.alignment
/*:
1. Puppy8Int
2. 8
3. Puppy
-----------------------------------------------------------------
|...|...|...|...|...|...|...|...|...| | | | | | | |
|...|...|...|...|...|...|...|...|...| | | | | | | |
|...|...|...|...|...|...|...|...|...| | | | | | | |
-----------------------------------------------------------------
Int Bool
|-----------> <----------|---|
// Size is the number of bytes to read from the pointer to reach all the data.
|-------------> size <--------------|
size = 9
*/
struct Puppy {
let age: Int
let isTrained: Bool
} // Int, Bool
MemoryLayout<Puppy>.size
MemoryLayout<Puppy>.stride
MemoryLayout<Puppy>.alignment
/*:
1. AlternatePuppy8Int
2. 8
3. AlternatePuppy
-----------------------------------------------------------------
|...| | | | | | | |...|...|...|...|...|...|...|...|
|...| | | | | | | |...|...|...|...|...|...|...|...|
|...| | | | | | | |...|...|...|...|...|...|...|...|
-----------------------------------------------------------------
|----------> Bool <----------|----------> Int <-----------|
// Size is the number of bytes to read from the pointer to reach all the data.
|---------------------------> size <----------------------------|
size = 16
*/
struct AlternatePuppy {
let isTrained: Bool
let age: Int
} // Bool, Int
MemoryLayout<AlternatePuppy>.size
MemoryLayout<AlternatePuppy>.stride
MemoryLayout<AlternatePuppy>.alignment
//: [Next](@next)