结构体与类的区别
This commit is contained in:
Binary file not shown.
@@ -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>
|
58
07 结构体.playground/Contents.swift
Normal file
58
07 结构体.playground/Contents.swift
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import UIKit
|
||||||
|
|
||||||
|
var greeting = "Hello, playground"
|
||||||
|
|
||||||
|
//: ### 结构体
|
||||||
|
/**
|
||||||
|
在 Swift 标准库中,绝大多数的公开类型都是结构体,而枚举和类只占很小一部分,比如Bool、Int、Double、 String、Array、Dictionary等常见类型都是结构体
|
||||||
|
|
||||||
|
- 所有的结构体都有一个编译器自动生成的初始化器(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
|
4
07 结构体.playground/contents.xcplayground
Normal file
4
07 结构体.playground/contents.xcplayground
Normal 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>
|
7
07 结构体.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
07 结构体.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Workspace
|
||||||
|
version = "1.0">
|
||||||
|
<FileRef
|
||||||
|
location = "group:">
|
||||||
|
</FileRef>
|
||||||
|
</Workspace>
|
Binary file not shown.
@@ -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>
|
16
08 类.playground/Contents.swift
Normal file
16
08 类.playground/Contents.swift
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import UIKit
|
||||||
|
|
||||||
|
//: ### 类
|
||||||
|
/*:
|
||||||
|
类的定义于结构体类似,编译器并没有为类自动生成可以传入成员值的初始化器
|
||||||
|
初始化器
|
||||||
|
- 类必须要有初始化器
|
||||||
|
- 如果类的所有成员都在定义的时候指定了初始值,编译器会为类生成无参的初始化器
|
||||||
|
- 成员的初始化是在这个初始化器中完成的
|
||||||
|
*/
|
||||||
|
class Point {
|
||||||
|
var x: Int = 0
|
||||||
|
var y: Int = 0
|
||||||
|
}
|
||||||
|
// 无参的初始化器
|
||||||
|
let p1 = Point()
|
32
08 类.playground/Resources/README.md
Normal file
32
08 类.playground/Resources/README.md
Normal 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()方法在内存中的存储结构
|
||||||
|

|
||||||
|
* point结构体是值类型,在内存中存储的是值
|
||||||
|
* size类是引用类型,在内存中存储的是Size对象的内存地址
|
||||||
|
|
||||||
|

|
||||||
|
* 前边16个字节存储的是指向类型信息以及引用计数
|
||||||
|
* 后边的16个字节存储的是成员变量
|
||||||
|
|
||||||
|
|
||||||
|
|
4
08 类.playground/contents.xcplayground
Normal file
4
08 类.playground/contents.xcplayground
Normal 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>
|
7
08 类.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
08 类.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Workspace
|
||||||
|
version = "1.0">
|
||||||
|
<FileRef
|
||||||
|
location = "group:">
|
||||||
|
</FileRef>
|
||||||
|
</Workspace>
|
BIN
08 类.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
BIN
08 类.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
Binary file not shown.
Reference in New Issue
Block a user