]> Zhao Yanbai Git Server - acecode.git/commitdiff
Classes & Properties
authorAceVest <zhaoyanbai@126.com>
Wed, 22 Jun 2016 15:42:37 +0000 (23:42 +0800)
committerAceVest <zhaoyanbai@126.com>
Wed, 22 Jun 2016 15:42:37 +0000 (23:42 +0800)
learn/AcePlay/AcePlay.playground/Pages/Classes.xcplaygroundpage/Contents.swift [new file with mode: 0644]
learn/AcePlay/AcePlay.playground/Pages/Properties.xcplaygroundpage/Contents.swift [new file with mode: 0644]
learn/AcePlay/AcePlay.playground/contents.xcplayground
learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate

diff --git a/learn/AcePlay/AcePlay.playground/Pages/Classes.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Classes.xcplaygroundpage/Contents.swift
new file mode 100644 (file)
index 0000000..7a81ad8
--- /dev/null
@@ -0,0 +1,61 @@
+//: [Previous](@previous)
+
+import UIKit
+
+printLine("Classes & Structures")
+
+struct ResolutionA {
+    var width = 0
+    var height: Int
+}
+
+// 如果成员未初始化,在实例化的时候要初始化所有成员
+var resolutionA = ResolutionA(width: 2, height: 3)
+
+
+struct Resolution {
+    var width  = 0
+    var height = 0
+}
+var resolutionB = Resolution()  // 成员初始化完了则不用
+
+let vga = Resolution(width: 640, height: 480)
+let hd  = Resolution(width: 1920, height: 1080)
+var cinema = hd
+cinema.width  = 2048
+
+// Struct 是值类型
+print("HD: \(hd.width)x\(hd.height)")
+print("Cinema: \(cinema.width)x\(cinema.height)")
+
+
+class VideoMode {
+    var resolution = Resolution()
+    var interlaced = false
+    var frameRate = 0.0
+    var name: String?
+}
+
+let someVideoMode = VideoMode()
+someVideoMode.resolution.width  = 1680  // Class是引用类型,所以纵然someVideoMode是常量,但其指向的是变量
+someVideoMode.resolution.height = 1050  // 属性访问是可以级联进行的
+
+let tenEighty = VideoMode()
+tenEighty.resolution = hd
+tenEighty.interlaced = true
+tenEighty.frameRate  = 25.0
+tenEighty.name       = "1080i"
+
+let anotherTenEighty = tenEighty         // 引用赋值
+anotherTenEighty.frameRate = 30.0
+print("Now TenEight Frame Rate: \(tenEighty.frameRate)")
+
+
+printLine("Identity Operators")
+if anotherTenEighty === tenEighty {
+    print("tenEighty & anotherTenEighty refer to the same VideoMode instance")
+}
+
+if someVideoMode !== tenEighty {
+    print("someVideoMode & tenEighty refer to different VideoMode instance")
+}
\ No newline at end of file
diff --git a/learn/AcePlay/AcePlay.playground/Pages/Properties.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Properties.xcplaygroundpage/Contents.swift
new file mode 100644 (file)
index 0000000..e636094
--- /dev/null
@@ -0,0 +1,78 @@
+//: [Previous](@previous)
+
+import UIKit
+
+printLine("Properies")
+
+printLine("Stored Properties")
+
+//Instances of FixedLengthRange have a variable stored property called firstValue and a constant stored property called length.
+struct FixedLengthRange {
+    var firstValue: Int
+    let length: Int
+}
+
+var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
+rangeOfThreeItems.firstValue = 7
+
+
+printLine("Stored Properties of Constant Structure Instances")
+
+let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
+//以下这句是不行的,因为rangeOfFourItems是常量FixedLengthRange的struct类型,是值类型,其值不能修改。而类因为是引用类型则可以。
+//rangeOfFourItems.firstValue = 9
+
+printLine("Lazy Stored Properties")
+// lazy property must always be variable
+// lazy 属性在声明前加上 lazy 关键字
+// 如果一个被标记为 lazy 的属性在没有初始化时就同时被多个线程访问,则无法保证该属性只会被初始化一次
+
+
+printLine("Computed Properties")
+
+struct Point {
+    var x = 0.0, y = 0.0
+}
+
+struct Size {
+    var width  = 0.0
+    var height = 0.0
+}
+
+struct Rect {
+    var origin = Point()
+    var size   = Size()
+    
+    var center: Point {
+        get {
+            var p = Point()
+            p.x = origin.x + size.width/2
+            p.y = origin.y + size.width/2
+            return p
+        }
+        
+        set(p) {
+            origin.x = p.x - size.width/2
+            origin.y = p.y - size.height/2
+        }
+        
+        /*
+         //set 有更简洁的写法,set重命名参数的话默认参数名为newValue
+         
+        set {
+            origin.x = newValue.x - size.width/2
+            origin.y = newValue.y - size.height/2
+        }
+        */
+    }
+}
+
+
+
+var square = Rect(origin: Point(x:0.0, y:0.0), size: Size(width: 10.0, height: 10.0))
+let squareCenter = square.center
+print("square center is (\(squareCenter.x), \(squareCenter.y))")
+
+square.center = Point(x: 15.0, y: 15.0)
+print("square new origin is (\(square.origin.x), \(square.origin.y))")
+
index 04644e5717ffaad6190a50887613b8d25706545d..d6d3e1a699042a2b652be7fa913780bfe147381c 100644 (file)
@@ -5,5 +5,7 @@
         <page name='Basics'/>
         <page name='Closure'/>
         <page name='Enumerations'/>
+        <page name='Classes'/>
+        <page name='Properties'/>
     </pages>
 </playground>
\ No newline at end of file
index f12efa0bb7e9602b4f04d60e044dfebad40714b7..1e2cf8b5d704e250a1767a62b314a19b37e33180 100644 (file)
Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ