diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..347ecb1 Binary files /dev/null and b/.DS_Store differ diff --git a/06 可选项.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate b/06 可选项.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate index 761741b..5f069d3 100644 Binary files a/06 可选项.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate and b/06 可选项.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/06 可选项.playground/xcuserdata/jiulinxiri.xcuserdatad/xcschemes/xcschememanagement.plist b/06 可选项.playground/xcuserdata/jiulinxiri.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..070bc3a --- /dev/null +++ b/06 可选项.playground/xcuserdata/jiulinxiri.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,16 @@ + + + + + SchemeUserState + + 06 可选项 (Playground).xcscheme + + isShown + + orderHint + 0 + + + + diff --git a/07 结构体.playground/Contents.swift b/07 结构体.playground/Contents.swift new file mode 100644 index 0000000..6066923 --- /dev/null +++ b/07 结构体.playground/Contents.swift @@ -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.alignment) // 8 +print(MemoryLayout.stride) // 24 +print(MemoryLayout.size) // 17 diff --git a/07 结构体.playground/contents.xcplayground b/07 结构体.playground/contents.xcplayground new file mode 100644 index 0000000..cf026f2 --- /dev/null +++ b/07 结构体.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/07 结构体.playground/playground.xcworkspace/contents.xcworkspacedata b/07 结构体.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/07 结构体.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/07 结构体.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate b/07 结构体.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..ce49906 Binary files /dev/null and b/07 结构体.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/07 结构体.playground/xcuserdata/jiulinxiri.xcuserdatad/xcschemes/xcschememanagement.plist b/07 结构体.playground/xcuserdata/jiulinxiri.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..157ef89 --- /dev/null +++ b/07 结构体.playground/xcuserdata/jiulinxiri.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,16 @@ + + + + + SchemeUserState + + 07 结构体 (Playground).xcscheme + + isShown + + orderHint + 0 + + + + diff --git a/08 类.playground/Contents.swift b/08 类.playground/Contents.swift new file mode 100644 index 0000000..89a6622 --- /dev/null +++ b/08 类.playground/Contents.swift @@ -0,0 +1,16 @@ +import UIKit + +//: ### 类 +/*: + 类的定义于结构体类似,编译器并没有为类自动生成可以传入成员值的初始化器 + 初始化器 + - 类必须要有初始化器 + - 如果类的所有成员都在定义的时候指定了初始值,编译器会为类生成无参的初始化器 + - 成员的初始化是在这个初始化器中完成的 + */ +class Point { + var x: Int = 0 + var y: Int = 0 +} +// 无参的初始化器 +let p1 = Point() diff --git a/08 类.playground/Resources/README.md b/08 类.playground/Resources/README.md new file mode 100644 index 0000000..23ce721 --- /dev/null +++ b/08 类.playground/Resources/README.md @@ -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个字节存储的是成员变量 + + + diff --git a/08 类.playground/contents.xcplayground b/08 类.playground/contents.xcplayground new file mode 100644 index 0000000..cf026f2 --- /dev/null +++ b/08 类.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/08 类.playground/playground.xcworkspace/contents.xcworkspacedata b/08 类.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/08 类.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/08 类.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate b/08 类.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..c1e45bc Binary files /dev/null and b/08 类.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate differ