Skip to content

Commit

Permalink
update new blog
Browse files Browse the repository at this point in the history
  • Loading branch information
platojobs committed Apr 9, 2024
1 parent afd8d1f commit 905e508
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
104 changes: 104 additions & 0 deletions BACKUP/184_swift的enum的内存布局研究.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# [swift的enum的内存布局研究](https://github.com/platojobs/SFLOG/issues/184)

```swift
enum Password {
case number(Int, Int,Int,Int)
case other
}

var pwd = Password.number(9, 8, 6, 4)
pwd = .other
print(MemoryLayout.stride(ofValue: pwd)) // 40 系统分配的
print(MemoryLayout.size(ofValue: pwd)) // 33 实际用到的
print(MemoryLayout.alignment(ofValue: pwd)) // 8 对齐单位

enum TestRnum {
case test1 , test2,test3
}

var tt = TestRnum.test1
print(MemoryLayout.stride(ofValue: tt)) // 1
print(MemoryLayout.size(ofValue: tt)) //1
print(MemoryLayout.alignment(ofValue: tt)) //1

enum TestRnum11{
case test(Int)
}
var tt11 = TestRnum11.test(12)
print(MemoryLayout.stride(ofValue: tt11)) // 8
print(MemoryLayout.size(ofValue: tt11)) //8
print(MemoryLayout.alignment(ofValue: tt11)) //8


enum TestEnum22 {
case test1(Int,Int,Int)
case test2(Int,Int)
case test3(Int)
case test4(Bool)
}

var tt22 = TestEnum22.test1(1, 2, 3)
print(MemoryLayout.stride(ofValue: tt22)) // 32
print(MemoryLayout.size(ofValue: tt22)) // 25
print(MemoryLayout.alignment(ofValue: tt22)) // 8


enum TestEnum33 {
case test0
case test1
case test2(Int , Int)
case test3(Int,Int,Int)
case test4(Int,Int,Int,Bool)
}

var test33 = TestEnum33.test0

print(MemoryLayout.stride(ofValue: test33)) // 32
print(MemoryLayout.size(ofValue: test33)) // 25
print(MemoryLayout.alignment(ofValue: test33)) // 8


enum TestEnum44 {
case test0
case test1
case test2(Int , Int)
case test3(Int,Int,Int)
case test4(Int,Bool,Int,Int)
}

var test44 = TestEnum44.test0

print(MemoryLayout.stride(ofValue: test44)) // 40
print(MemoryLayout.size(ofValue: test44)) // 33
print(MemoryLayout.alignment(ofValue: test44)) // 8


enum TestEnum55 {
case test0
case test1
case test2(Int ,Int)
case test3(Int,Int,Int)
case test4(Bool,Int,Int,Int)
}
var test55 = TestEnum55.test0
print(MemoryLayout.stride(ofValue: test55)) // 40
print(MemoryLayout.size(ofValue: test55)) // 33
print(MemoryLayout.alignment(ofValue: test55)) // 8


enum Test00{
case enum1(t1:Int)
case enum2(t1:Int8,t2:Int16,t3:Int) // 1,2,8
case enum3(t1:Int8,t2:Int,t3:Int16,t4:Int32)
case enum4(t1:Int8,t2:Int,t3:Int16,t4:Int,t5:Int8,t6:String) //(1,8,2,8,1,16)
}


var testddd = Test00.enum1(t1: 1)

print(MemoryLayout.stride(ofValue: testddd)) // 56
print(MemoryLayout.size(ofValue: testddd)) // 56
print(MemoryLayout.alignment(ofValue: testddd)) // 8


```
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,17 @@ My personal blog using issues and GitHub Actions (哈哈)
- [Widget 的两种类型](https://github.com/platojobs/SFLOG/issues/195)--2022-05-09
- [swift-enum](https://github.com/platojobs/SFLOG/issues/194)--2022-05-08
- [Rxswift的smaple用法](https://github.com/platojobs/SFLOG/issues/186)--2022-04-28
- [RXSwift的merge](https://github.com/platojobs/SFLOG/issues/185)--2022-04-28
- [swift的enum的内存布局研究](https://github.com/platojobs/SFLOG/issues/184)--2022-04-26
- [Quartz 2D绘图中常见的函数及其功能](https://github.com/platojobs/SFLOG/issues/117)--2022-02-15
- [swift跳出语句](https://github.com/platojobs/SFLOG/issues/89)--2022-01-17
- [随手记](https://github.com/platojobs/SFLOG/issues/84)--2022-01-08
- [57:swiftc常用命令](https://github.com/platojobs/SFLOG/issues/61)--2021-11-30
- [55:BlockOperation](https://github.com/platojobs/SFLOG/issues/59)--2021-10-25
- [54: swift的函数](https://github.com/platojobs/SFLOG/issues/58)--2021-10-21
- [53: swift中枚举的内存布局](https://github.com/platojobs/SFLOG/issues/57)--2021-10-21
<details><summary>显示更多</summary>

- [54: swift的函数](https://github.com/platojobs/SFLOG/issues/58)--2021-10-21
- [53: swift中枚举的内存布局](https://github.com/platojobs/SFLOG/issues/57)--2021-10-21
- [33云:swift的Mirror的内部实现,修改代码实现探索](https://github.com/platojobs/SFLOG/issues/33)--2021-08-27
- [19云:iOS 13.0以后presentViewController](https://github.com/platojobs/SFLOG/issues/19)--2021-08-07
</details>
Expand Down

0 comments on commit 905e508

Please # to comment.