]> Zhao Yanbai Git Server - acecode.git/commitdiff
swift-subscripts
authoracevest <zhaoyanbai@126.com>
Tue, 14 Mar 2017 04:56:37 +0000 (12:56 +0800)
committeracevest <zhaoyanbai@126.com>
Tue, 14 Mar 2017 04:56:37 +0000 (12:56 +0800)
learn/AcePlay/AcePlay.playground/Pages/CollectionTypes.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Methods.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Subscripts.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

index 78be21ea0a2033652178183ba388b95698e0fa33..f3c040e12c71bf9347c615f2b2116c3ea05e3d87 100644 (file)
@@ -2,7 +2,7 @@
 
 import Foundation
 
-var str = "Hello, playground"
+var str = "Hello, CollectionTypes"
 
 //: [Next](@next)
 
index 745b4066ae753fb12e9cd243ca3ca64c77c981ab..09c1ce834568269dc4990ed74f10c1c1c9835ff2 100644 (file)
@@ -2,7 +2,7 @@
 
 import Foundation
 
-var str = "Hello, playground"
+var str = "Hello, Methods"
 
 //: [Next](@next)
 
diff --git a/learn/AcePlay/AcePlay.playground/Pages/Subscripts.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Subscripts.xcplaygroundpage/Contents.swift
new file mode 100644 (file)
index 0000000..8bf935f
--- /dev/null
@@ -0,0 +1,80 @@
+//: [Previous](@previous)
+
+import Foundation
+
+var str = "Hello, Subscripts"
+
+//: [Next](@next)
+
+// 下标可以定义在类、结构体和枚举中
+
+struct TimesTable {
+    let multiplier: Int
+    // subscript(index: Int) -> Int {
+    //      get {
+    //          返回一个适当的 Int 类型的值
+    //      }
+    //      set(newValue) {
+    //          执行适当的赋值操作
+    //      }
+    // }
+
+    // 只读下标实现如下
+    subscript(index: Int) -> Int {
+        return multiplier * index
+    }
+}
+
+let sevenTimesTable = TimesTable(multiplier: 7)
+print("six times seven is \(sevenTimesTable[6])")
+
+
+
+class Matrix {
+    let rows: Int
+    let cols: Int
+    var grid: [Double]
+    
+    init(rows: Int, cols: Int) {
+        self.rows = rows
+        self.cols = cols
+        grid = Array(repeating: 0.0, count: rows * cols)
+    }
+    
+    func indexIsValid(row: Int, col: Int) -> Bool {
+        return row >= 0 && row < rows && col >= 0 && col < cols
+    }
+    
+    subscript(row: Int, col: Int) -> Double {
+        get {
+            assert(indexIsValid(row: row, col: col))
+            return grid[row*cols + col]
+        }
+        
+        set {
+            assert(indexIsValid(row: row, col: col))
+            grid[row*cols + col] = newValue
+        }
+    }
+    
+    var description: String {
+        var s : String = ""
+        for i in 0..<rows {
+            for j in 0..<cols {
+                s += "Matrix[\(i), \(j)] = \(grid[i*rows + j]) "
+            }
+            s += "\n"
+        }
+        
+        return s
+    }
+}
+
+
+var matrix = Matrix(rows: 2, cols: 2)
+
+print(matrix.description)
+matrix[0, 1] = 1.414
+matrix[1, 1] = 1.732
+print(matrix.description)
\ No newline at end of file
index 52e276fba5a2a03e87cde75a3449f8b77cf6344e..7a38434ccc95e7fe333ec543b3321de2ea7567c5 100644 (file)
@@ -10,5 +10,6 @@
         <page name='CollectionTypes'/>
         <page name='ControlFlow'/>
         <page name='Methods'/>
+        <page name='Subscripts'/>
     </pages>
 </playground>
\ No newline at end of file
index 5d871e6e33c3a54256d6fa30308f8be2d4ec59ea..e07186e958dbf8c34d82a238656037c97603a052 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