结构体与类的区别

This commit is contained in:
2022-01-06 23:03:08 +08:00
parent 2162005745
commit 28a8532422
13 changed files with 160 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>06 可选项 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@@ -0,0 +1,58 @@
import UIKit
var greeting = "Hello, playground"
//: ###
/**
Swift ,BoolIntDouble StringArrayDictionary
- initializer
-
*/
struct Date {
//
var year: Int
var month: Int
var day: Int
}
//
var date = Date(year: 2021, month: 12, day: 14)
struct Point {
var x: Int = 0
var y: Int
}
var p1 = Point(x: 10, y: 10)
// p2使 Point(y), x
var p2 = Point(y: 10)
//: ####
/*:
-
*/
struct PointA {
var x: Int = 0
var y: Int = 0
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
// PointA
var p3 = PointA(x: 12, y: 12)
//: ####
struct PointB {
var x: Int = 0 // 8
var y: Int = 0
var origin: Bool = false
}
print(MemoryLayout<PointB>.alignment) // 8
print(MemoryLayout<PointB>.stride) // 24
print(MemoryLayout<PointB>.size) // 17

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>07 结构体 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@@ -0,0 +1,16 @@
import UIKit
//: ###
/*:
-
-
-
*/
class Point {
var x: Int = 0
var y: Int = 0
}
//
let p1 = Point()

View File

@@ -0,0 +1,32 @@
### 结构体与类的本质区别
* 结构体是值类型(枚举也是值类型),类是引用类型(指针类型)
值类型存储在栈空间,引用类型存储在堆空间
```swift
// 类
class Size {
var width = 1
var height = 2
}
// 结构体
struct Point {
var x = 3
var y = 4
}
// 方法体
func test() {
var size = Size()
var point = Point()
}
```
test()方法在内存中的存储结构
![jiulinxiri_20220106223910.png](https://vip1.loli.io/2022/01/06/5nON1A42PkVFijr.png)
* point结构体是值类型在内存中存储的是值
* size类是引用类型在内存中存储的是Size对象的内存地址
![jiulinxiri_20220106224431.png](https://vip1.loli.io/2022/01/06/TRshqXCfrilFnQu.png)
* 前边16个字节存储的是指向类型信息以及引用计数
* 后边的16个字节存储的是成员变量

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>